0
votes

I have a fairly complex application which makes use of custom authentication middleware. I have a route group like this:

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

    Route::get('/', function() {

        echo 'private';
    });

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

        Route::get('/', function() {

            echo 'public';
        });
    })
});

Now the auth middleware will redirect all requests which are not authenticated. The main group with the auth prefix is authenticated. However, I want the group public to be accessible even if the user is not authenticated.

So:

http://example.com/auth < must be authenticated
http://example.com/auth/some-sub-page < must be authenticated
http://example.com/auth/public < no need for authentication

So is there a way to add something like 'remove-middleware' => 'auth' to the nested group with the public prefix? Or would I have to restructure my route groups?

2

2 Answers

1
votes

Why not just wrap the auth middleware around the non-public routes?

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

    // routes requiring auth
    Route::group(['middleware' => 'auth']) {

        Route::get('/', function() {

            echo 'private';
        });

    }

    // other routes
    Route::group(['prefix' => 'public'], function() {

        Route::get('/', function() {

            echo 'public';
        });

    });
});

There may be a way to do a 'remove-middleware' type middleware, but that could get messy. This seems like the cleanest solution.

0
votes

you can use withoutMiddleware method on your route.

Route::get('/example')->withoutMiddleware('auth')

You can also use withoutMiddleware on Route::group