Laravel 路由组和中间件
Laravel route group and middlewares
我正在与可以验证状态(已验证电子邮件)的用户一起开发 laravel 项目。
另一方面,用户可以通过“订阅”中间件验证订阅。
所以我有几组路由,其中 2 组唯一的区别是是否存在订阅
第 1 组:
Route::group(['middleware' => ["auth:sanctum", "verified"]], function () {}
组 2
Route::group(['middleware' => ["auth:sanctum", "verified", "subscriptions"]], function () {}
我的问题是关于 laravel 用于这些组中路由的顺序。例如,如果用户满足第一组的所有中间件,laravel 是否测试第二组的中间件?
已验证用户是否有机会进入第二组订阅路线?
反之,如果用户没有订阅,则不会通过订阅中间件。但我的印象是用户被失败的订阅中间件重定向,而 laravel 可以在没有这个中间件的组中找到正确的路由(第 1 组)
我想要的是它只测试是否存在订阅,如果没有找到,它会在 group1 中查找路由。
代码中组的顺序对处理有影响吗?
谢谢。
编辑:
Route::group(['middleware' => ["auth:sanctum", "verified", ]], function () {
Route::get("/new", function () {
// redirect to payment
})->name("new-payment");
}
Route::group(['middleware' => ["auth:sanctum", "verified", "subscriptions"]], function () {
Route::get("/new", function () {
return view("bourse-new");
})->name("new-abo");
这是相同的路由,但根据是否存在订阅而具有不同的行为
当订阅中间件失败时,它会重定向到“home”,但我希望 laravel 使用第一个路由
在app/Http/Kernel.php
protected $middlewarePriority = [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
文档:
https://laravel.com/docs/8.x/middleware#sorting-middleware
my question is about the order laravel uses for routes in these
groups. for example if the user satisfies all the middleware of the
first group, does laravel test the middleware of the second?
如果您击中了第一组中包含的路线 laravel 将不会检查另一组。
Does a
user verified have a chance to enter the second group of routes with
subscription?
如果他没有订阅他就不能
Does the order of the groups in the code have an impact on the processing?
是的,如果您使用相同的 uri,最后一个将覆盖前一个
Route::group(['middleware' => ["auth:sanctum", "verified", ]], function () {
Route::get("/new", function () {
// redirect to payment
})->name("new-payment");
}
Route::group(['middleware' => ["auth:sanctum", "verified", "subscriptions"]], function () {
Route::get("/new", function () {
return view("bourse-new");
})->name("new-abo");
尝试 php artisan route:list,第二组可能会覆盖第一组
ok thanks, but I want a different behavior depending on the presence
or not of a subscription
在用户模型中:
public function hasSubscribed()
{
//do your logic to check if has subscription
}
您可以使用第一组并检查 Auth::user() 是否已订阅。像这样:
if (auth()->user()->hasSubscribed()) {
return view()
}
//or ...
感谢@NoobDev。
我通过将订阅测试集成到中间件中接管了他的解决方案
Route::group(['middleware' => ["auth:sanctum", "verified"]], function () {
Route::get("/new", function () {
return view("bourse-new");
})->middleware("subscriptions")->name("bourse-new");
});
和订阅中间件:
public function handle(Request $request, Closure $next){
if( //logic tests) {
return $next($request);
}
return redirect('/checkout'); //redirect to payment
}
这个方案几乎完美,谢谢大家
我正在与可以验证状态(已验证电子邮件)的用户一起开发 laravel 项目。
另一方面,用户可以通过“订阅”中间件验证订阅。
所以我有几组路由,其中 2 组唯一的区别是是否存在订阅
第 1 组:
Route::group(['middleware' => ["auth:sanctum", "verified"]], function () {}
组 2
Route::group(['middleware' => ["auth:sanctum", "verified", "subscriptions"]], function () {}
我的问题是关于 laravel 用于这些组中路由的顺序。例如,如果用户满足第一组的所有中间件,laravel 是否测试第二组的中间件? 已验证用户是否有机会进入第二组订阅路线?
反之,如果用户没有订阅,则不会通过订阅中间件。但我的印象是用户被失败的订阅中间件重定向,而 laravel 可以在没有这个中间件的组中找到正确的路由(第 1 组)
我想要的是它只测试是否存在订阅,如果没有找到,它会在 group1 中查找路由。
代码中组的顺序对处理有影响吗?
谢谢。
编辑:
Route::group(['middleware' => ["auth:sanctum", "verified", ]], function () {
Route::get("/new", function () {
// redirect to payment
})->name("new-payment");
}
Route::group(['middleware' => ["auth:sanctum", "verified", "subscriptions"]], function () {
Route::get("/new", function () {
return view("bourse-new");
})->name("new-abo");
这是相同的路由,但根据是否存在订阅而具有不同的行为 当订阅中间件失败时,它会重定向到“home”,但我希望 laravel 使用第一个路由
在app/Http/Kernel.php
protected $middlewarePriority = [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
文档: https://laravel.com/docs/8.x/middleware#sorting-middleware
my question is about the order laravel uses for routes in these groups. for example if the user satisfies all the middleware of the first group, does laravel test the middleware of the second?
如果您击中了第一组中包含的路线 laravel 将不会检查另一组。
Does a user verified have a chance to enter the second group of routes with subscription?
如果他没有订阅他就不能
Does the order of the groups in the code have an impact on the processing?
是的,如果您使用相同的 uri,最后一个将覆盖前一个
Route::group(['middleware' => ["auth:sanctum", "verified", ]], function () {
Route::get("/new", function () {
// redirect to payment
})->name("new-payment");
}
Route::group(['middleware' => ["auth:sanctum", "verified", "subscriptions"]], function () {
Route::get("/new", function () {
return view("bourse-new");
})->name("new-abo");
尝试 php artisan route:list,第二组可能会覆盖第一组
ok thanks, but I want a different behavior depending on the presence or not of a subscription
在用户模型中:
public function hasSubscribed()
{
//do your logic to check if has subscription
}
您可以使用第一组并检查 Auth::user() 是否已订阅。像这样:
if (auth()->user()->hasSubscribed()) {
return view()
}
//or ...
感谢@NoobDev。
我通过将订阅测试集成到中间件中接管了他的解决方案
Route::group(['middleware' => ["auth:sanctum", "verified"]], function () {
Route::get("/new", function () {
return view("bourse-new");
})->middleware("subscriptions")->name("bourse-new");
});
和订阅中间件:
public function handle(Request $request, Closure $next){
if( //logic tests) {
return $next($request);
}
return redirect('/checkout'); //redirect to payment
}
这个方案几乎完美,谢谢大家