douhao2548 2019-01-26 17:53
浏览 96

Laravel 5 - 新的Route ::资源跟随前一个

I just started with Laravel on my project and I face some problems in routes. Currently, I made tutorial resource route:

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

route:list gives me the following output:

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

Everythings works perfectly, now I would like to add some mini-series to some tutorials, I would like to add a new resource route and leave my route like this:

// 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

At this point I was able to make it creating all routes manually:

// 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']);

As you may notice there is a lot of code here, I would like to ask if someone know if there is some additional option that would simplify this code. I would like to add a second Route::resource and leave it clean, but when I give it a try I got the following output:

// 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条回答 默认 最新

  • doupingpeng7567 2019-01-26 18:06
    关注

    In my understanding to use route resource, the controller must be created as resource controller as well. In my case

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

    This is how i use route resource. If you create manually then it has to be as how you did above. Try looking at route groups as well. Correct me if i am wrong TQ

    评论

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?