stack stack - 1 month ago 9
PHP Question

How to apply an middleware on ::auth()?

Here is my code in the

routes.php
file:

Route::auth();


Now I need to put a middleware on the way of that route. Something like this:

Route::auth()->middleware('setLang');


But in this case, it throws this error:


FatalErrorException in routes.php line 43:
Call to a member function middleware() on null


How can I fix it?

Answer

To make it work, you first need to register middleware.

After that use group() method to set middleware property:

Route::group(['middleware' => 'setLang'], function () {
    Route::auth();
});

If you still get Class not found error, try to run composer dumpauto command.