中间件认证除了抛出错误方法

Middleware auth except throwing error method

我想要向来宾显示视图:listingsshowlisting 页面(无需登录)。

使用时:

$this->middleware('auth'; 

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

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

我收到这个错误:

BadMethodCallException Method App\Http\Controllers\ListingsController::middlware does not exist.

我找了几天,没找到解决方法

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();

你有:

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

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

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