4

Im wondering if there is a way to specify static routes, along with a way to dynamically look up a root level page url, something like below would be in my routes.php file

Route::get('admin/users', array('before' => 'isAdmin', 'uses' => 'UserController@userAdministration'));
Route::post('admin/users', array('before' => 'isAdmin', 'uses' => 'UserController@userList'));

Route::post('profile', 'UserController@profile');

Route::get('{dynamic_page}','PageController@getPage');

How would I set this up?

Gabriel Alack
  • 472
  • 2
  • 6
  • 16

2 Answers2

1

I am not clear what you are asking, either. But an example of a dynamic route would be as follows (notice the param customer_id and how it is used):

Route::get('{customer_id}/profile', 'CustomerController@getProfile')->where('customer_id', '[0-9]+');

A root url route might look like:

Route::get('/', array('as' => 'root', 'uses' => 'RootController@showRoot'));

Again, I'm not entirely sure what you mean by 'dynamically looking up a root url'.

Joel Joel Binks
  • 1,628
  • 5
  • 27
  • 47
1

The best way to approach this depends on what you are trying to do with your "dynamic" routes.

Your routes work just fine as written, in that GET or POST calls to the URLs http://example.com/admin/users and http://example.com/profile will be routed to the UserController, while any other "root" level GET (e.g. calling http://example.com/some_page) will route to the getPage method of the PageController. From there, you can access the {dynamic_page} parameter, and act on it however you wish:

class PageController extends BaseController {
    public function getPage($dynamic_page) {
        // do something here with $dynamic_page:
        //
        // for example, return a view based on $dynamic_page
        //      return View::make($dynamic_page);
        //
        // or retrieve a row from the database based on $dynamic_page
        //      $page = new Page($dynamic_page);

    return ('The page is '.$dynamic_page);        
    }
}

Presumably, in your PageController, you will need to do something with $dynamic_page to make sure a valid response is available—run it through a switch statement, perhaps, check for an available View, or query the database—otherwise returning a NotFoundHttpException.

It would probably be better to handle some of this logic in routes.php—for instance, requiring {dynamic_page} to fulfill certain requirements—a numeric ID, perhaps, by adding

->where('dynamic_page', '[0-9]+'); (as Joel suggested above).

Note, also, that the way you have it set up will only accept a "root-level" wildcard; calls with additional parameters, such as http://example.com/some_page/something_else, will fail, returning a NotFoundHttpException and/or leading to a 404 error page.

damiani
  • 7,071
  • 2
  • 23
  • 24