Laravel 5-新的Route :: resource紧随前一个

时间:2019-01-26 17:53:44

标签: php laravel-5.7

我刚开始在项目中使用Laravel,在路线上遇到一些问题。目前,我已经制作了教程资源路线:

Route::resource('/tutorials', 'TutorialsController')->parameters(['tutorial' => 'id']);

route:list提供了以下输出:

POST      | tutorials                
GET|HEAD  | tutorials                
GET|HEAD  | tutorials/create         
DELETE    | tutorials/{tutorial}     
PUT|PATCH | tutorials/{tutorial}     
GET|HEAD  | tutorials/{tutorial}     
GET|HEAD  | tutorials/{tutorial}/edit

一切正常,现在我想在一些教程中添加一些迷你剧集,我想添加一条新的资源路线并按如下方式离开我的路线:

// Tutorials
POST      | tutorials                             
GET|HEAD  | tutorials                             
GET|HEAD  | tutorials/create                      
DELETE    | tutorials/{tutorial}                  
PUT|PATCH | tutorials/{tutorial}                  
GET|HEAD  | tutorials/{tutorial}                  
GET|HEAD  | tutorials/{tutorial}/edit             

// Mini-series of tutorials
POST      | tutorials/{tutorial}               
GET|HEAD  | tutorials/{tutorial}/create        
GET|HEAD  | tutorials/{tutorial}/{mini-serie}     
DELETE    | tutorials/{tutorial}/{mini-serie}     
PUT|PATCH | tutorials/{tutorial}/{mini-serie}     
GET|HEAD  | tutorials/{tutorial}/{mini-serie}/edit

在这一点上,我能够使其手动创建所有路线:

// Mini-series routes:
Route::post('tutorials/{tutorial}', 'SeriesController@store')->name('serie.store');
Route::get('tutorials/{tutorial}/create', 'SeriesController@create')->name('serie.create');
Route::get('tutorials/{tutorial}/{mini-serie}', 'SeriesController@show')->name('serie.show');
Route::delete('tutorials/{tutorial}/{mini-serie}', 'SeriesController@destroy')->name('serie.destroy');
Route::patch('tutorials/{tutorial}/{mini-serie}', 'SeriesController@update')->name('serie.update');
Route::get('tutorials/{tutorial}/{mini-serie}/edit', 'SeriesController@edit')->name('serie.edit');

// Tutorials routes:
Route::resource('/tutorials', 'TutorialsController')->parameters(['tutorial' => 'id']);

您可能会注意到这里有很多代码,我想问是否有人知道是否有一些其他选项可以简化此代码。我想添加第二个Route::resource并保持干净,但是当我尝试一下时,得到以下输出:

// Route::resource('/tutorials/{tutorial}', 'SeriesController')->parameters(['tutorial' => 'id']);
POST      | tutorials/{tutorial}                  
GET|HEAD  | tutorials/{tutorial}                  
GET|HEAD  | tutorials/{tutorial}/create           
GET|HEAD  | tutorials/{tutorial}/{{tutorial}}     
DELETE    | tutorials/{tutorial}/{{tutorial}}     
PUT|PATCH | tutorials/{tutorial}/{{tutorial}}     
GET|HEAD  | tutorials/{tutorial}/{{tutorial}}/edit

1 个答案:

答案 0 :(得分:0)

据我了解,要使用路由资源,必须同时将控制器创建为资源控制器。就我而言

   Route::resource('staff/regulatory', 'Regulatory\LicensingController',[
        'names' => 
        [
             'index'    => 'staff.approval.index', 
             'show'     => 'staff.approval.show', 
             'store'    => 'staff.approval.store',   

        ]
]); 

这是我使用路线资源的方式。如果手动创建,则必须与上面的操作相同。也尝试查看路线组。纠正我,如果我错了TQ