首页 > 解决方案 > 中间件认证除了抛出错误方法

问题描述

我希望为客人显示视图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();

标签: laravel-5.8

解决方案


你有:

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

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

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

推荐阅读