Creating dynamic routes

Submitted by bfalcao - 6 years ago

Sometimes we need to create dynamic routes based on database information. Here is an example that you can use on your projects, mostly inside your routes file, by retrieving a routes array (as example) and load them dynamically. Have fun! Bruno twitter.com/brunocfalcao

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

// For instance this can come from your database.
$paths = ['path/to/route1','path/to/route2','path/to/route3'];

// Then iterate the router "get" method.
foreach($paths as $path)
    $router->get($path, 'PilotsController@index')->name('pilots.index');
    
    
/**
  You can also use:
  router->resource
  router->post
  router->delete
  router->match
  ...
*/