I'm having a bit of a problem by using a middleware inside a group that has a middleware itself like 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 AuthenticationMiddleware
<?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: So, he enters this last middleware event when outside the /checkout scope. How can I avoid it ? Thanks all
From your comments I see you added your middleware on both $middleware
AND $routeMiddleware
, thus the AuthenticationMiddleware
will run on every request. If you only want your request to pass AuthenticationMiddleware
on your specified route(s), then remove it from $middleware
and only keep it in $routeMiddleware
.
From the documentation:
If you want a middleware to be run during every HTTP request to your application, simply list the middleware class in the $middleware property of your app/Http/Kernel.php class.
and:
If you would like to assign middleware to specific routes, you should first assign the middleware a short-hand key in your app/Http/Kernel.php file. By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel. To add your own, simply append it to this list and assign it a key of your choosing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With