Php Laravel 5-身份验证中间件不保护路由,但抛出错误消息

Php Laravel 5-身份验证中间件不保护路由,但抛出错误消息,php,laravel,Php,Laravel,我的auth中间件有一些问题。我想用它来保护我的路线,但在一些控制器中它确实不起作用。 如果我作为注销用户检查路由,我会得到不同的错误消息,而不是阻止访问并重定向到登录页面。 我总是在控制器中的_construct()上使用中间件 举个例子 控制器 public function show(Dialog $dialog) { return $this->template($dialog, self::DISPLAY_MODE_DIALOG); } protected functi

我的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'));
}
模型

错误

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

现在我明白了为什么会发生这种情况,但我认为中间件不应该显示这些消息,而是应该保护路由

但是,如果用户经过身份验证,中间件将允许请求进一步进入应用程序


我用ryanmortier的解决方案解决了我的问题。 从疯狂中走出来的路对我不起作用


您能用控制器中的构造函数来扩展您的问题吗?我总是这样使用中间件
code
public function\uu construct(){$this->middleware('auth);}
code
您正在路由中使用模型绑定,因此它会在控制器之前、中间件之前被调用。有没有办法修复它?如果您在
中检查用户是否可访问(user$user=null)
。。。然后
if($user).
set accessible,如果用户为null,则返回false
public function isAccessibleBy(User $user)
{
    $profile = $this->moderator()->appProfile();
    return  $profile->isOwner($user->id)
                    || $profile->isControlledBy($user->id);
}