Laravel Creating Dynamic Routes to controllers from Mysql database

后端 未结 5 647
无人及你
无人及你 2020-12-07 11:14

I have the following table: group_pages in mysql database with page name route name :

   id   name      route
  --------------------
    0   About      about         


        
5条回答
  •  Happy的楠姐
    2020-12-07 12:03

    try

    Route::get('/', ['as' => 'home', 'uses' => 'HomeController@index']);
    
    $pages = 
    Cache::remember('pages', 5, function() {
        return DB::table('pages')
                ->where('status', 1)
                ->lists('slug');
    
    });
    
    if(!empty($pages)) 
    {
      foreach ($pages as $page)
      {
        Route::get('/{'.$page.'}', ['as' => $page, 'uses' => 'PagesController@show']);
       }
    }
    

提交回复
热议问题