Support the ongoing development of Laravel.io →
posted 9 years ago
Requests
Last updated 10 months ago.
0

From the docs http://laravel.com/docs/routing#route-filters

Route::get('user', array('before' => 'auth|old', function()
{
    return 'You are authenticated and over 200 years old!';
}));

I'm assuming you could do a group the same way, but I it's untested.

Last updated 10 months ago.
0

Thanks, I know about that option. But I have several that need other filtering. That's why I want to nest them and break it down.

Last updated 10 months ago.
0

@MacWorks, you need to add "prefix" to your route group. Here is section of my route file.

Route::group(['prefix' => 'admin', 'namespace' => 'Controllers\Admin'], function()
{
    // /admin/dashboard
    Route::get('dashboard', [
        'as' => 'admin_dashboard', 
        'uses' => '[email protected]'
    ]);

    ...

    // /admin/members
    Route::group(['prefix' => 'members'], function()
    {
        Route::get('/', [
            'as' => 'admin_members', 
            'uses' => '[email protected]'
        ]);
        
        // /admin/members/search
        Route::get('search', [
            'as' => 'admin_member_search', 
            'uses' => '[email protected]'
        ]);

        Route::get('{id}/status', [
            'as' => 'admin_member_inactive', 
            'uses' => '[email protected]'
        ]);

        Route::post('{id}/remove', [
           'before' => 'isGod',
            'as' => 'admin_member_remove', 
            'uses' => '[email protected]'
        ]);
        
        Route::get('mailinglist', [
            'as' => 'admin_member_maillist', 
            'uses' => '[email protected]'
        ]);

    });
    
    ...
});
Last updated 10 months ago.
0

Can you filter based on prefixes?

Last updated 10 months ago.
0

Yes you can. Pattern Based Filters

Route::when('admin/*', 'admin', array('post'));
Last updated 10 months ago.
0

You can also nest groups as explained here: http://www.laravel-tricks.com/tricks/route-group-namespacing Hope it helps!

Last updated 10 months ago.
0

I also have this issue.

I have route group inside another route group working.

But if I now need to have another route group that the initial route groups must be within, as I need to apply a filter to all routes. I can't add the filter to the first group as the filter is needed before a prefix is added.

So is there some way around this nesting problem? I don't understand how name space groups will help me fix this, but I am quite new to Laravel.

Update: Ofcourse I found the answer soon after making the post.

My problem was that I was using groups for filters in combination with individual filters. It seems you can't apply 3 levels of nested grouping and then a filter to an individual route.

So my solution was to make all routes in groups for each filter, and have no filters applied to an individual route. This may well be documented, but I missed it.

Last updated 10 months ago.
0

A working example of nested groups with no prefix or namespace

I needed guest filter applied to all 4 routes but csrf only for the post routes.

Route::group(['before' => 'guest'], function()
{
    //only require the guest filter
    Route::get('login', ['as' => 'login.create', 'uses' => '[email protected]']);
    Route::get('register', ['as' => 'register.create', 'uses' => '[email protected]']);

    //require both guest and csrf filter
    Route::group(['before' => 'csrf'], function()
    {
        Route::post('login', ['as' => 'login.store', 'uses' => '[email protected]']);
        Route::post('register', ['as' => 'register.store', 'uses' => '[email protected]']);
    });
});
Last updated 10 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

MacWorks macworks Joined 6 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 Laravel.io - All rights reserved.