Laravel Group Inside Middleware Group

I have a problem using middleware within a group that has middleware, such as the following code:

Route::group(['prefix' => '{lang?}','middleware'=>'language'], function() { Route::get('/', ' HomeController@index '); Route::get('/login',' AuthController@login '); Route::post('/login',' AuthController@do _login'); Route::get('/logout',' AuthController@logout '); Route::group(['prefix' => 'checkout','middleware'=>'authentication'], function () { Route::get('/', " CheckoutController@step1 "); }); }); 

And my current authentication is Middleware

 <?php namespace App\Http\Middleware; use Closure; use Illuminate\Contracts\Routing\Middleware; use Session; use App; use Redirect; class AuthenticationMiddleware{ /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { die("inside"); if(!User::check()) { return Redirect::to("/login"); } else { return $next($request); } } } 

EDIT: Thus, it enters this last intermediate event when it is outside the scope of verification. How can i avoid this? Thank you all

+4
source share
1 answer

From your comments, I see that you added your middleware as on $middleware AND $routeMiddleware , so AuthenticationMiddleware will start on every request. If you want your request to pass AuthenticationMiddleware to the specified route, remove it from $middleware and save it only in $routeMiddleware .

From the documentation:

If you want the middleware to be launched during each HTTP request to your application, just list the middleware class in the middleware of your application property / class Http / Kernel.php.

and:

If you want to assign middleware to specific routes, you must first assign middleware with a shortcut key in your app / Http / Kernel.php. By default, the $ routeMiddleware property of this class contains entries for the middleware included with Laravel. To add your own, simply add it to this list and assign it the key of your choice.

+1
source

All Articles