是否可以使用单个路由通过控制器访问多个页面?

时间:2019-08-09 08:17:37

标签: laravel laravel-5 eloquent laravel-5.8

我有多个页面需要通过数据库中的id访问它们,我发现很难在刀片模板中访问它们。我决定创建一个可以访问它们的单个控制器,那么有可能做到这一点吗?

我的控制器:

string GetMessage(string name)

路线:

 public function  index(Request $request,$id)
 {
         $categories  = Category::with('subcategories')
                      ->get();
         return View::make(['index','pages.quote','pages.video','pages.music','pages.radio','pages.inspiration','pages.gospel','pages.image'], [
         'categories'=>$categories
     ]);

 }

刀片视图:

 Route::get('test','NewsletterController@test');
 Route::get('newsletter','NewsletterController@create');
 Route::post('newsletter','NewsletterController@store');
 Route::get('/', 'MainController@index')->name('index');
 Route::get('quote/{id}','HeaderController@index');
 Route::get('video/{id}','HeaderController@index');
 Route::get('radio/{id}','HeaderController@index');
 Route::get('inspiration/{id}','HeaderController@index');
 Route::get('gospel/{id}','HeaderController@index');
 Route::get('image/{id}','HeaderController@index');

1 个答案:

答案 0 :(得分:0)

我不知道您想要达到什么目的,但是您可以这样做:

 public function  index(Request $request,$id){
     $categories  = Category::with('subcategories')->get();
     if ($id == 3){ // example
       $page = 'pages.quote';
     }elseif($id == 4){ // example
       $page = 'pages.video';
     }else{
       $page = 'index';
     }
     return View::make($page, ['categories'=>$categories]);
 }