Reputation: 11
Is it possible to name a Group of Routes?
Something like:
Route::group(array('as'=>'fruits'), function(){
Route::get('apple', array('as'=>'apple','uses'=>'fruits@apple'));
Route::post('apple', array('uses'=>'fruits@editapple'));
Route::get('pear', array('as'=>'pear', 'uses'=>'fruits@pear'));
});
Then check if the URL is "fruits" by doing:
if (Request::route()->is('fruits')){
// One of the "fruits" routes is active
}
Or do I have to:
Route::get('fruits/apple', array('as'=>'apple','uses'=>'fruits@apple'));
Route::post('fruits/apple', array('uses'=>'fruits@editapple'));
Route::get('fruits/pear', array('as'=>'pear', 'uses'=>'fruits@pear'));
Then check by:
if(URI::is('fruits/*')){
//"fruits" active
}
It's for a navmenu.
Upvotes: 1
Views: 807
Reputation: 4111
Can't see if you are using Laravel 3 or Laravel 4. With Laravel 4 you can use Route Prefixing
Route::group(array('prefix' => 'fruits'), function()
{
Route::get('apple', array('as'=>'apple','uses'=>'fruits@apple'));
Route::post('apple', array('uses'=>'fruits@editapple'));
Route::get('pear', array('as'=>'pear', 'uses'=>'fruits@pear'));
});
You can check it by using this
if(Request::is('fruits/*')) {
// One of the "fruits" routes is active
}
When you are using Laravel 3, I think you have to create an bundle named fruits so you have the url prefix.
Then you can check the active route by this way
if(URI::is('fruits/*')){
//"fruits" active
}
Upvotes: 2
Reputation: 146191
You can't name a group using your first example but I think you can do it but in a different way (sharing my idea, don't know right or wrong) , tested only in version 3
Routes.php
Route::any('/fruits/(:any)', function($fruite){
// Pass a parameter to the method, for example (demo purpose only)
$param_for_method = $fruite == 'apple' ? 'Green' : 'Yellow';
// Call the controller method, $fruite will represent (:any)
Controller::call("fruits@$fruite", array($param_for_method));
});
Your Controller :
class Fruits_Controller extends Base_Controller
{
public function action_apple($args)
{
//
}
public function action_banana($args)
{
//
}
// you can create as many fruit's method as you want
}
Now, if we write http://yourdomain.dev/fruits/apple
then it'll call the apple
method from our fruits
controller and the parameter will be Green
that is accessible using $args
and if we write http://yourdomain.dev/fruits/banana
then you know the rest.
Upvotes: 0