2013-05-01 84 views
1

是否可以命名一组路线?Laravel命名的群组路线

喜欢的东西:

Route::group(array('as'=>'fruits'), function(){ 
    Route::get('apple', array('as'=>'apple','uses'=>'[email protected]')); 
    Route::post('apple', array('uses'=>'[email protected]')); 
    Route::get('pear', array('as'=>'pear', 'uses'=>'[email protected]')); 
}); 

然后检查URL为 “水果” 做:

if (Request::route()->is('fruits')){ 
    // One of the "fruits" routes is active 
} 

还是我必须到:

Route::get('fruits/apple', array('as'=>'apple','uses'=>'[email protected]')); 
Route::post('fruits/apple', array('uses'=>'[email protected]')); 
Route::get('fruits/pear', array('as'=>'pear', 'uses'=>'[email protected]')); 

然后,通过检查:

if(URI::is('fruits/*')){ 
    //"fruits" active 
} 

这是一个navmenu。

回答

0

使用你的第一个例子你不能说出一组,但我认为你可以做到这一点,但以不同的方式(分享我的想法,不知道是对还是错),在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("[email protected]$fruite", array($param_for_method)); 
}); 

控制器:

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 
} 

现在,如果我们写http://yourdomain.dev/fruits/apple那么它就会从fruits控制器调用apple方法和参数将Green可访问使用$args,如果我们写http://yourdomain.dev/fruits/banana那么你都知道了。

2

不能看到,如果你正在使用Laravel 3或Laravel 4. Laravel 4您可以使用Route Prefixing

Route::group(array('prefix' => 'fruits'), function() 
{ 
    Route::get('apple', array('as'=>'apple','uses'=>'[email protected]')); 
    Route::post('apple', array('uses'=>'[email protected]')); 
    Route::get('pear', array('as'=>'pear', 'uses'=>'[email protected]')); 
}); 

您可以使用此

if(Request::is('fruits/*')) { 
    // One of the "fruits" routes is active 
} 

当你检查它正在使用Laravel 3,我认为你必须创建一个名为水果的包,这样你才有了url前缀。

然后你就可以通过这种方式

if(URI::is('fruits/*')){ 
    //"fruits" active 
} 
+0

谢谢你的答案检查活动路线。我对拉拉维尔还是一个新手,所以会仔细研究如何制作一个包。 – GlomB 2013-05-02 13:06:26

+0

签出http://laravel.com/docs/bundles#creating-bundles上的文档。如果你只是盯着你的应用程序,那么最好转换到Laravel 4.现在它是beta版本,但它们在本月发布。 – JackPoint 2013-05-02 13:08:17

+0

啊,是的,资源控制器就是我一直在寻找的东西。接下来的问题将是切换到Laravel 4 :) – GlomB 2013-05-03 16:49:05