Laravel模态绑定导致路由未找到错误


Laravel modal binding gives route not found error

我正在使用laravel 4.2开发简单粗暴的应用程序。这是我用于编辑/更新的控制器方法。

class ProductsController extends BaseController{
    public function getEdit($id){
       $product=Products::find($id);
       $this->layout->content=View::make('products.edit',compact('product'));   
   }
}

这是edit.blade.php文件的一部分

 {{ Form::model($product, ['route' => ['products/update', $product->id], 'method' => 'patch']) }}

我在route.php文件中为ProductsController定义了如下路由

 Route::controller ( 'products', 'ProductsController');

当我尝试编辑产品时(http://localhost:8000/products/5/edit)

它说路线[产品/更新]没有定义

这是我的编辑链接

 <a class="btn btn-small btn-info" href="{{ URL::to('products/' . $product->id . '/edit') }}">Edit </a>

这个错误的原因是什么?我已经在产品contller上定义了patchUpdate()函数。

您使用的是路由控制器,而不是资源丰富的控制器,因此没有"命名"路由。

你可以做这个

{{ Form::model($product, ['action' => 'ProductsController@putEdit', $product->id], 'method' => 'patch']) }}

添加以下行您的routes.php文件

Route::model('products', 'Product');
Route::resource('products', 'ProductsController');

并改变@The Shift Exchange建议的

products.update not products/update

也更改

 <a class="btn btn-small btn-info" href="{{ URL::to('products/getEdit/'. $product->id) }}">Edit </a>