简体   繁体   中英

Laravel group admin routes

Is there a way to cleanly group all routes starting with 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 .

Thanks!

Unfortunately no. Route groups were not designed to work like that. This is taken from the Laravel docs.

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:

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

The handles key allows you to change what URI the bundle will respond to. So in this case any calls to admin will be run through that bundle. Then in your new bundle create a routes.php file and you can register the handler using the (:bundle) placeholder.

// 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 :

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');
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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