gpt4 book ai didi

组中间件内的 Laravel 组

转载 作者:行者123 更新时间:2023-12-04 21:38:55 24 4
gpt4 key购买 nike

通过在具有中间件本身的组中使用中间件,我遇到了一些问题,如下代码所示:

    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");
});
});

和我当前的 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);
}
}

}

编辑:
因此,他在/checkout 范围之外时进入了最后一个中间件事件。我怎样才能避免它?
谢谢大家

最佳答案

从您的评论中,我看到您在 $middleware 上都添加了中间件和 $routeMiddleware ,因此 AuthenticationMiddleware将在每个请求上运行。如果您只想让您的请求通过 AuthenticationMiddleware在您指定的路线上,然后将其从 $middleware 中删除并且只保存在 $routeMiddleware .

从文档:

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.



和:

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.

关于组中间件内的 Laravel 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com