Laravel Creating Dynamic Routes to controllers from Mysql database

后端 未结 5 639
无人及你
无人及你 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条回答
  •  自闭症患者
    2020-12-07 11:58

    We can make dynamic route by this way

    // Instanciate a router class.
    $router = app()->make('router');
    

    Get Route value from Database

    // For route path this can come from your database.
    $paths = ['path_one','path_two','path_three'];
    

    Then iterate the value to make dynamic route

    // Then iterate the router "get" method.
    foreach($paths as $path){
        $router->resource($path, 'YourController');
    }
    

    You can use GET|POST|PUT|PATCH|DELETE methods also

    // Then iterate the router "get" method.
    foreach($paths as $path){
        $router->get($path, 'YourController@index')->name('yours.index');
    }
    

提交回复
热议问题