中间件身份验证,但抛出错误的方法除外

时间:2019-05-04 22:02:55

标签: laravel-5.8

我希望向访客显示listingsshowlisting页面(无需登录)。

使用时:

$this->middleware('auth'; 

在我的ListingsController的构造函数中,一切正常(对于已登录的用户),但是当我使用以下方法排除索引并显示方法时:

$this->middlware('auth')->except('index','show');

我收到此错误:

  

BadMethodCallException方法   App \ Http \ Controllers \ ListingsController :: middlware不存在。

我搜索了几天,但没有找到任何解决方法。

ListingsController.php

public function __construct()
{
    $this->middlware('auth')->except('index', 'show');
}

web.php(路由文件)

Route::get('/', 'ListingsController@index');
Route::resource('listings', 'ListingsController');
Route::get('/dashboard', 'DashboardController@index');
Auth::routes();

1 个答案:

答案 0 :(得分:0)

您有:

$this->middlware('auth')->except('index', 'show');

中间件拼写错误,您的错误反映了这一点。应该是:

$this->middleware('auth')->except(['index', 'show']);