A. Apola
A. Apola

Reputation: 131

LARAVEL: How to use middleware on named routes

I've been working on an app that initially didn't use middleware. Later on, I decided to add middleware and had to change my routes from something like:

Route::get('admin/poems', array('as' => 'poems', 'uses' =>      'PoemsController@poem'));

to

Route::get('admin/poem', ['middleware' => 'auth', 'uses' => 'PoemsController@poem']);

Now the disadvantage is that I had been redirecting to this route (poems) several times and adding middleware as indicated will require me to go through all my code and change the name of the route in the redirect. How do i solve this problem?

Thanks for any help.

Upvotes: 0

Views: 411

Answers (2)

James
James

Reputation: 16339

You don't need to lose the name of your route, the array will still accept it along with your middleware.

Just add it in to look like so:

Route::get('admin/poem', ['middleware' => 'auth', 'as' => 'poems', 'uses' => 'PoemsController@poem']);

This way you don't need to go through and rename your routes anywhere and can still protect it with auth middleware.

Upvotes: 2

Raymond Cheng
Raymond Cheng

Reputation: 2505

try put middleware to a group route

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function ()    {
        // Uses Auth Middleware
    });

    Route::get('user/profile', function () {
        // Uses Auth Middleware
    });
});

Upvotes: 0

Related Questions