问题描述
我是Laravel和Stackoverflow的新手
Laravel7发布后,我开始学习Laravel。 新版本8.0是不久前推出的,我开始尝试。
当我尝试以下操作(edit.blade.PHP)
{!! Form::open(['action' => ['ProductController@update',$product->id],'method' => 'POST']) !!}
或
{!! Form::open(['action' => [[ProductController::class,'update'],'method' => 'POST']) !!}
发生错误
未定义操作ProductController @ update
然后我尝试用类似的路径替换控制器名称
{!! Form::open(['action' => ['App\Http\Controllers\ProductController@update','method' => 'POST']) !!}
或
{!! Form::open(['action' => [[App\Http\Controllers\ProductController::class,'method' => 'POST']) !!}
有效!
所以我认为这与名称空间有关
但是我有一个名称空间标题
namespace App\Http\Controllers;
在我的App \ Http \ ProductController.PHP
尽管我可以通过以集体形式键入控制器的完整路径来解决问题, 我担心我的代码有配置错误或语法错误等。
解决方法
这是对Laravel 8的更改。默认情况下,没有名称空间前缀应用于您的路由,并且在生成指向“操作”的URL时不使用名称空间前缀。
要在要生成操作的URL时尝试在Controller前面添加命名空间,您需要在$namespace
上定义App\Providers\RouteServiceProvider
属性:
protected $namespace = 'App\Http\Controllers';
现在,您可以使用该名称空间前缀引用您的操作:
Form::open(['action' => ['ProductController@update',$product->id],'method' => 'POST'])
,
在 Laravel 8 中:使用此代码我认为您的问题将得到解决
web.php
Route::middleware(['auth:sanctum','verified'])->group(function () {
Route::get('create','Product\ProductController@Create')->name('product.create')->middleware('can:Add Product');
Route::post('create','Product\ProductController@Store')->name('product.store')->middleware('can:Add Product');
});
资源/浏览量/产品:
<form method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">
@csrf
{{-- START - PRODUCT INFORMATION --}}
{{-- END- PRODUCT INFORMATION --}}
</form>
应用/Http/控制器/产品:
<?php
namespace App\Http\Controllers\Products;
use App\Http\Requests\Product\ProductRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ProductController extends Controller {
//---Constructor Function
public function __construct() {
$this->middleware('auth:sanctum');
}//---End of Function Constructor
//---Create Product
public function create() {
return view('product.product_add');
}//---End of Function create
//---Store Product in Database
public function store(ProductRequest $request) {
//---Add Product Details in DB
}
?>
,
Laravel 8 : Collective form - 用于创建 Laravel 视图页面
{!! Form::open(['route' => '','id'=>'form','class' => 'needs-validation',]) !!}
<div class="form-group row">
{!! Form::label('employee_id','Employee ID:',['class'=>'col-md-1 col-form-label custom_required']) !!}
<!-- Employee ID -->
<div class="form-group row">
{!! Form::label('employee_name','Employee Name:',['class'=>'col-md-1 col-form-label custom_required']) !!}
<div class="col-lg-8">
{!! Form::text('employee_name',@$employee_id,['class' => 'form-control','required','placeholder' => 'Employee Name','pattern'=> '^[a-z A-Z0-9_.-]*$']) !!}
</div>
<!-- Employee number -->
<div class="form-group row">
{!! Form::label('phone_number','Number:',['class'=>'col-md-1 col-form-label custom_required']) !!}
<div class="col-lg-8">
{!! Form::text('phone_number',@$phone_number,'placeholder' => 'number']) !!}
</div>
{!! Form::close() !!}