我正在尝试一个REST设计,但我遇到了一个小问题。我有一个资源schedule。因此,/schedules/{id}的常规表示法不太适用,因为我希望先使用/schedules/{day}/{month}/{year},然后应用REST,然后使用/edit等。
有没有办法用Route::resource()做到这一点?或者我需要通过Route::get()来完成这些操作?
发布于 2014-02-16 02:23:57
据我所知,route::resource只提供documentation中详细描述的路由,因此您需要声明自己的路由。它仍然是restful的,如果它只是您想要更改的足智多谋的路由之一,您应该仍然能够执行以下操作,因为这些路由是按照它们声明的顺序排列优先级的。
Route::get('schedule/{day}/{month}/{year}/edit', array('as' => 'editSchedule', 'uses' => 'ScheduleController@edit'));
Route::resource('schedule', 'ScheduleController');发布于 2015-08-18 06:20:03
是的,有一个非常简单的方法。下面是一个示例:
如下所示指定您的路由:
Route::resource("schedules/day.month.year", "ScheduleController");请求将如下所示:
/schedules/day/1/month/12/year/2014现在你可以在你的控制器的show方法中获得所有这三个参数:
public function show($day, $month, $year)发布于 2015-12-17 08:11:19
嗨,这可能是方便的,如果你想叫你的路线的名字。您还可以使用一个或多个参数。我在laravel 5.1上也能用
根据laravel docs:http://laravel.com/docs/5.1/routing#named-routes
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
//
}]);
$url = route('profile', ['id' => 1]);这也适用于Route:resource。
例如:
Route::resource('{foo}/{bar}/dashboard', 'YourController');将创建命名路由,如:{foo}.{bar}.dashboard.show
要使用route方法调用它,您可以按如下方式设置它。
route('{foo}.{bar}.dashboard.show', ['foo' => 1, 'bar'=> 2]),它将创建url yourdomain.com/1/2/dashboard
我希望这是有用的。
Pascal
https://stackoverflow.com/questions/21792472
复制相似问题