简体   繁体   English

Laravel中间件不起作用

[英]Laravel middleware not working

I have a middleware class :- 我有一个中间件课程:-

<?php

namespace App\Http\Middleware;


use Closure;

class isAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(auth()->user()->getIsAdminAttribute()){

            return $next($request);


        }
        abort(404);
    }
}

In Kernel.php file:- 在Kernel.php文件中:-

<?php

namespace App\Http;

use App\Http\Middleware\isAdmin;

protected $routeMiddleware = [
        'isAdmin' => isAdmin::class,
    ];

In web.php route file:- 在web.php路由文件中:

Route::get('users/list', 'UsersController@listUsers')->name('List_Users')->middleware('isAdmin');

I've tried to dump the 我试图把

dd(auth()->user()->getIsAdminAttribute());

Nothing happen , like I am not assigning isAdmin middleware to that route at all. 什么也没发生,就像我根本没有将isAdmin中间件分配给该路由一样。

You could check your routes and middleware assigned to them with this command : 您可以使用以下命令检查分配给它们的路由和中间件:

php artisan route:list

Should look like : 应该看起来像:

+--------+----------+-------------+------------+-------------------------------------------------+--------------+
| Domain | Method   | URI         | Name       | Action                                          | Middleware   |
+--------+----------+-------------+------------+-------------------------------------------------+--------------+
|        | GET|HEAD | /           |            | Closure                                         | web          |
|        | GET|HEAD | users/list  | List_Users | App\Http\Controllers\UsersController@listUsers  | web,isAdmin  |
+--------+----------+-------------+------------+-------------------------------------------------+--------------+

Note the isAdmin in the middleware column. 注意中间件列中的isAdmin。

我知道对您来说太晚了,但是也许其他人会从中受益,我遇到了同样的问题,经过一些调试后,我发现中间件问题并未被触发,并且由于Route :: get(而出现在route:list中)即使我将此路线放在遇到问题Route :: get('post / create')的路线之后,也将显示路线从post更改为post,然后再次触发中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM