Laravel 5 - Auth Middleware不保护路由但抛出错误消息

时间:2015-10-26 09:17:34

标签: php laravel

我的auth中间件存在一些问题。我想用它来保护我的路线,但在某些控制器中,它确实很有效。 如果我将路由检查为注销用户,我会得到不同的错误消息,而不是阻止访问并重定向到登录页面。 我总是在控制器中的__construct()上使用中间件。

一个例子。

控制器

public function show(Dialog $dialog)
{
    return $this->template($dialog, self::DISPLAY_MODE_DIALOG);
}

protected function template(Dialog $dialog, $displayMode, $messageLimit = 10)
{
    $user = Auth::user();

    // Check if the profile is currently controlled by the current user
    $isControlled = $dialog->moderator()->appProfile()->isControlledBy($user->id);
    $messageList  = $dialog->latestMessages($messageLimit);

    return view('dialog.chat.template')
        ->with(compact('dialog', 'messageList', 'isControlled', 'displayMode'));
}

模型

public function isAccessibleBy(User $user)
{
    $profile = $this->moderator()->appProfile();
    return  $profile->isOwner($user->id)
                    || $profile->isControlledBy($user->id);
}

错误

  

传递给App \ Model \ Dialog \ Dialog :: isAccessibleBy()的参数1必须是App \ Model \ Account \ User的实例,null给定,在C:\ projekte \ php \ newchat \ app \ Model \中调用Dialog \ Dialog.php在332行并定义了

现在我意识到为什么会发生这种情况,但我认为中间件应该不会显示此消息并保护路线。

http://laravel.com/docs/5.0/middleware

  

“但是,如果用户通过身份验证,则中间件将允许请求继续进入应用程序。”

1 个答案:

答案 0 :(得分:0)

我通过ryanmortier解决了我的问题。 来自LPMadness的方式适合我。

https://github.com/laravel/framework/issues/6118