Laravel 路由的自定义方法而不是资源
Custom method instead of resource for Laravel routes
使用 4.2 并尝试向我的控制器添加自定义方法。
我的路线是:
Route::get('ticket/close_ticket/{id}', 'TicketController@close_ticket');
Route::resource('ticket', 'TicketController');
一切 CRUD 明智的工作,但在我的 TicketController 的底部,我有这个基本功能:
public function close_ticket($id) {
return "saved - closed";
}
当我在我的页面上显示 link 路由时:
{{ link_to_route('ticket/close_ticket/'.$ticket->id, 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
我经常收到路线未定义的错误,但它确实已定义...?
知道哪里出了问题吗?
laravel 辅助方法 link_to_route
生成一个 HTML link。这意味着当点击时,用户将执行 GET
请求。
在您的路线文件中,您将其定义为 POST
路线。
Route::post(...)
此外,请在此处查看 link_to_route
的文档:
http://laravel.com/docs/4.2/helpers
您会看到第一个参数应该只是路由名称,没有附加 ID。
link_to_route
需要路由名称,而不是 url。这就是您收到 'route not defined' 错误的原因,因为您尚未使用提供给 link_to_route
的名称定义路由。如果你给你的路线一个名字,你可以使用 link_to_route
.
给定以下路由定义,路由名称现在为 'close_ticket':
Route::get('ticket/close_ticket/{id}', array('as' => 'close_ticket', 'uses' => 'TicketController@close_ticket'));
'as' 键的值是路由名称。这是要在 link_to_route
:
中使用的值
{{ link_to_route('close_ticket', 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
使用 4.2 并尝试向我的控制器添加自定义方法。
我的路线是:
Route::get('ticket/close_ticket/{id}', 'TicketController@close_ticket');
Route::resource('ticket', 'TicketController');
一切 CRUD 明智的工作,但在我的 TicketController 的底部,我有这个基本功能:
public function close_ticket($id) {
return "saved - closed";
}
当我在我的页面上显示 link 路由时:
{{ link_to_route('ticket/close_ticket/'.$ticket->id, 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
我经常收到路线未定义的错误,但它确实已定义...?
知道哪里出了问题吗?
laravel 辅助方法 link_to_route
生成一个 HTML link。这意味着当点击时,用户将执行 GET
请求。
在您的路线文件中,您将其定义为 POST
路线。
Route::post(...)
此外,请在此处查看 link_to_route
的文档:
http://laravel.com/docs/4.2/helpers
您会看到第一个参数应该只是路由名称,没有附加 ID。
link_to_route
需要路由名称,而不是 url。这就是您收到 'route not defined' 错误的原因,因为您尚未使用提供给 link_to_route
的名称定义路由。如果你给你的路线一个名字,你可以使用 link_to_route
.
给定以下路由定义,路由名称现在为 'close_ticket':
Route::get('ticket/close_ticket/{id}', array('as' => 'close_ticket', 'uses' => 'TicketController@close_ticket'));
'as' 键的值是路由名称。这是要在 link_to_route
:
{{ link_to_route('close_ticket', 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}