gpt4 book ai didi

laravel - 为什么在 Laravel 中不工作组中间件?

转载 作者:行者123 更新时间:2023-12-02 17:26:20 25 4
gpt4 key购买 nike

我在路由 Laravel 中使用了以下中间件:

Route::group(['middleware' => 'web'], function () {

Route::resource('Order', 'OrderController');
});

当我尝试在 URL 中调用此路径时:

http://localhost/web/order

我得到一个错误:

Sorry, the page you are looking for could not be found.

in RouteCollection.php line 161 at RouteCollection->match(object(Request)) in Router.php line 821 at Router->findRoute(object(Request)) in Router.php line 691 at Router->dispatchToRoute(object(Request)) in Router.php line 675 at Router->dispatch(object(Request)) in Kernel.php line 246 at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 at Pipeline->then(object(Closure)) in Kernel.php line 132 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 at Kernel->handle(object(Request)) in index.php line 53

最佳答案

Route::group();,顾名思义,用于对共享某些共同点的路由进行分组。

'middleware' => 'web' 会让里面的所有路由共享同一组中间件或者共享同一个中间件。查看 app/Http/Kernel.php

中间件不会影响您的路由 URL,但会影响该路由在您的应用中的处理方式。

'prefix' => 'web' 将使您的所有路由共享相同的路径。哪个看起来更符合您的需要。

正确的代码是:

Route::group(['prefix' => 'web'], function () {
Route::resource('Order', 'OrderController');
});

访问此路由的 URL 将是:

/web/order

要让 web 和中间件 auth 都带有 guard api auth:api 前缀,代码将是:

Route::group(['prefix' => 'web', 'middleware' => ['auth:api']], function () {
Route::resource('Order', 'OrderController');
});

关于laravel - 为什么在 Laravel 中不工作组中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38423439/

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