amo*_*mof 7 php resources routes laravel
我正在尝试使用资源构建我的路由,以便我可以将两个参数传递到我的资源中.
我将举几个URLS的外观示例:
domain.com/dashboard
domain.com/projects
domain.com/project/100
domain.com/project/100/emails
domain.com/project/100/email/3210
domain.com/project/100/files
domain.com/project/100/file/56968
Run Code Online (Sandbox Code Playgroud)
所以你可以看到我总是需要引用project_id以及电子邮件/文件ID等.
我意识到我可以通过手动编写所有路径来手动执行此操作,但我正在尝试坚持资源模型.
我觉得这样的事可能有用吗?
Route::group(['prefix' => 'project'], function(){
Route::group(['prefix' => '{project_id}'], function($project_id){
// Files
Route::resource('files', 'FileController');
});
});
Run Code Online (Sandbox Code Playgroud)
据我所知,资源
Route::resource('files', 'FileController');
Run Code Online (Sandbox Code Playgroud)
上面提到的资源将路由以下网址.
您的资源控制器处理的几个操作 Route::resource('files', 'FileController');
Route::get('files',FileController@index) // get req will be routed to the index() function in your controller
Route::get('files/{val}',FileController@show) // get req with val will be routed to the show() function in your controller
Route::post('files',FileController@store) // post req will be routed to the store() function in your controller
Route::put('files/{id}',FileController@update) // put req with id will be routed to the update() function in your controller
Route::delete('files',FileController@destroy) // delete req will be routed to the destroy() function in your controller
Run Code Online (Sandbox Code Playgroud)
resource上面提到的单一内容将完成所有列出的内容routing
除了那些你必须写你的 custom route
在您的场景中
Route::group(['prefix' => 'project'], function(){
Route::group(['prefix' => '{project_id}'], function($project_id){
// Files
Route::resource('files', 'FileController');
});
});
Run Code Online (Sandbox Code Playgroud)
domain.com/project/100/files
如果get请求将路由到FileController@index
其post请求将被路由到的请求FileController@store
如果您的" domain.com/project/100/file/56968"更改为" domain.com/project/100/files/56968" (文件到文件),则会发生以下生根...
domain.com/project/100/files/56968
如果get请求将被路由到FileController@show
其put请求将被路由到FileController@update
其delete请求将被路由到的请求FileController@destroy
它对url你提到的任何其他内容都没有影响
提供,您需要具有RESTful资源控制器
对于像'/ project/100/file/56968'这样的请求,您必须像这样指定您的路线:
Route::resource('project.file', 'FileController');
Run Code Online (Sandbox Code Playgroud)
然后你可以在控制器的show方法中获取参数:
public function show($project, $file) {
dd([
'$project' => $project,
'$file' => $file
]);
}
Run Code Online (Sandbox Code Playgroud)
这个例子的结果将是:
array:2 [?
"$project" => "100"
"$file" => "56968"
]
Run Code Online (Sandbox Code Playgroud)