Passing 2 Parameters to Laravel Routes - Resources

梦想的初衷 提交于 2019-12-03 07:10:28

As far as I know about resources

Route::resource('files', 'FileController');

The above mentioned resource will route the following urls.

Few Actions Handled By Resource Controller for your 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

the single resource Mentioned above will do all the listed routing

Apart from those you have to write your custom route

In your scenario of

Route::group(['prefix' => 'project'], function(){
  Route::group(['prefix' => '{project_id}'], function($project_id){

    // Files
    Route::resource('files', 'FileController');

  });
}); 

domain.com/project/100/files

if its a get request will be routed to FileController@index
if its a post request will be routed to FileController@store

if your "domain.com/project/100/file/56968" is changed to "domain.com/project/100/files/56968" (file to files)then the following rooting will occur...

domain.com/project/100/files/56968

if its a get request will be routed to FileController@show
if its a put request will be routed to FileController@update
if its a delete request will be routed to FileController@destroy

and it has no impact on any other urls you have mentioned

Provided, you need to have RESTful Resource Controllers

For the request like '/project/100/file/56968', you must specify your route like this:

Route::resource('project.file', 'FileController');

And then you can get parameters at the show method of the controller:

public function show($project, $file) {
    dd([
        '$project' => $project,
        '$file' => $file
    ]);
}

The result of this example will be:

array:2 [▼
  "$project" => "100"
  "$file" => "56968"
]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!