2013-05-01 82 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