php - laravel 4 - multiple routes to the same controller -
i trying avoid code:
route::get('/', array('as' => 'create_content', 'uses' => 'samecontroller@create')); route::get('create_content', array('as' => 'create_content', 'uses' => 'samecontroller@create'));
and combined them 1 route. because controller same.
so controller active when u go index "/" , "create_content".
and if here - maybe can elaborate me purpose of "as" in array?
thanks!
in laravel you're not restricted static values paths. can use {}'s denote variable. adding ? after variable name makes variable optional. these variables passed routing method (in case, "create" method of samecontroller).
this can combined method pull off want. method allows define regex restrictions on what's allowed variable.
another alternative use route::pattern, more "global" version of where(). i've included example of both convenience. :)
as 'as', you're able name route in laravel. after naming route 'as', can access through of laravel's useful functions, such url::route('nameofroute') or redirect::route('nameofroute');
a functioning example below:
route::pattern('mypattern', '(create_content)?'); route::get('/{path?}', array('as' => 'nameofroute', 'uses' => 'samecontroller@create') )->where('path', '(create_content)?'); // functionally equivalent above route::get. // route::get('/{mypattern?}', array('as' => 'nameofroute', 'uses' => 'samecontroller@create') // ); // example of how make use of 'as' defined above. route::get('create_more_content', function() { return redirect::route('nameofroute'); });
Comments
Post a Comment