2012-09-18 46 views
4

有没有一种方法可以将所有以admin/开头的路线分组? 我想这样的事情,但它没有ofcourse工作:Laravel组管理路线

Route::group('admin', function() 
{ 
    Route::get('something', array('uses' => '[email protected]')); 
    Route::get('another', array('uses' => '[email protected]')); 
    Route::get('foo', array('uses' => '[email protected]')); 
}); 

对应于这些路线:

admin/something 
admin/another 
admin/foo 

我可以ofcourse只需直接前缀的所有这些路由与admin/,但我想知道是否可以做到这一点我的方式

谢谢!

回答

3

不幸的是没有。路线组的设计并非如此。这是来自Laravel文档。

路由组允许您将一组属性附加到一组路由中,从而使您的代码保持整洁。

路由组用于将一个或多个过滤器应用于一组路由。你正在寻找的是捆绑!

介绍捆绑!

捆绑是你所追求的东西。在你的包目录下创建一个名为“管理员”的新包,并在你的应用程序/文件bundles.php注册为这样的事情:

'admin' => array(
    'handles' => 'admin' 
) 

的处理密钥允许你改变什么URI捆绑将响应。所以在这种情况下,任何对admin的呼叫都将通过该捆绑包运行。然后在你的新包中创建一个routes.php文件,你可以使用(:bundle)占位符注册处理程序。

// Inside your bundles routes.php file. 
Route::get('(:bundle)', function() 
{ 
    return 'This is the admin home page.'; 
}); 

Route::get('(:bundle)/users', function() 
{ 
    return 'This responds to yoursite.com/admin/users'; 
}); 

希望能给你一些想法。

+0

谢谢!没有考虑捆绑。 – EsTeGe

+0

仍然没有解决这个问题,但与捆绑?我在问Laravel 3 – Alex

3

Laravel 4您现在可以使用prefix

Route::group(['prefix' => 'admin'], function() { 

    Route::get('something', '[email protected]'); 

    Route::get('another', function() { 
     return 'Another routing'; 
    }); 

    Route::get('foo', function() { 
     return Response::make('BARRRRR', 200); 
    }); 

    Route::get('bazz', function() { 
     return View::make('bazztemplate'); 
    }); 

});