Laravel 5.5中间件不起作用

时间:2018-06-17 14:29:41

标签: php laravel laravel-5 laravel-middleware

我有一个名为let comments=[]; if(this.props.post.post_comment && this.props.post.post_comment.length>0) comments = this.props.post.post_comment.map((comment) => <li>{comment.comment_body}</li>); 的中间件,以下是代码:

IsAdmin

这里是在内核中注册它:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if ($user->role === 10)
        {
            Session::flash('error', 'Трябва да сте администратор за да видите тази страница.');
            return redirect('/home');
        }

        return $next($request);
    }
}

以下是我如何保护控制器中的路由:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'isadmin' => \App\Http\Middleware\IsAdmin::class,
    ];
}

public function __construct() { $this->middleware(['auth', 'isadmin']); } 中间件工作得很好。

当我在用户模型上更改数据库中使用的权限字段时,auth中间件停止工作。它曾经是“$ user-&gt; is_admin”,我已将其更改为“$ user-&gt; role”,现在甚至还原为旧方法并不能解决问题。

以下是用户模型的迁移:

isadmin

以下是我的路线或<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->tinyInteger('role')->default(0); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } 档案:

web.php

2 个答案:

答案 0 :(得分:0)

如果要将中间件包含在$routeMiddleware属性中,则可以使用中间件方法将中间件分配给路径:

试试这个

Route::get('/dashboard', 'HomeController@dashboard')->name('dashboard')->middleware(['isadmin','auth']);

或者您可以使用组方法

Route::group(['middleware' => ['isadmin', 'auth']], function () {
    //
});

答案 1 :(得分:0)

问题在于这一行:

if ($user->role === 10)

应该是这样的:

if ($user->role === 0)