简体   繁体   English

Laravel组管理员路线

[英]Laravel group admin routes

Is there a way to cleanly group all routes starting with admin/ ? 有没有一种方法可以对所有以admin/开头的路由进行干净的分组? I tried something like this, but it didn't work ofcourse: 我尝试了类似的方法,但是它当然没有用:

Route::group('admin', function()
{
    Route::get('something', array('uses' => 'mycontroller@index'));
    Route::get('another', array('uses' => 'mycontroller@second'));
    Route::get('foo', array('uses' => 'mycontroller@bar'));
});

Corresponding to these routes: 对应于以下路线:

admin/something
admin/another
admin/foo

I can ofcourse just prefix all those routes directly with admin/ , but I'd like to know if it's possible to do it my way . 我当然可以直接用admin/所有这些路由的前缀,但是我想知道是否有可能按照我的方式进行

Thanks! 谢谢!

Unfortunately no. 很不幸的是,不行。 Route groups were not designed to work like that. 路由组的设计并非如此。 This is taken from the Laravel docs. 这取自Laravel文档。

Route groups allow you to attach a set of attributes to a group of routes, allowing you to keep your code neat and tidy. 路由组使您可以将一组属性附加到一组路由,从而使代码保持整洁。

A route group is used for applying one or more filters to a group of routes. 路由组用于将一个或多个过滤器应用于一组路由。 What you're looking for is bundles! 您正在寻找的是捆绑包!

Introducing Bundles! 捆绑包介绍!

Bundles are what you're after, by the looks of things. 从外观上看,捆绑销售就是您的追求。 Create a new bundle called 'admin' in your bundles directory and register it in your application/bundles.php file as something like this: 在bundles目录中创建一个名为“ admin”的新bundle,并将其注册到application / bundles.php文件中,如下所示:

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

The handles key allows you to change what URI the bundle will respond to. 使用handles键可以更改捆绑软件将响应的URI。 So in this case any calls to admin will be run through that bundle. 因此,在这种情况下,对admin的任何调用都将通过该捆绑包运行。 Then in your new bundle create a routes.php file and you can register the handler using the (:bundle) placeholder. 然后在新的包中创建一个route.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';
});

Hope that gives you some ideas. 希望能给您一些想法。

In Laravel 4 you can now use prefix : Laravel 4您现在可以使用prefix

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

    Route::get('something', 'mycontroller@index');

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

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

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM