我正在开发一个基于Laravel 5.1的系统。我有一个路由资源:
Route::resource('applicant', 'ApplicantController');
如我们所料,它在控制器中有以下功能:
index, create, store, edit, update, delete
我想要的是只在index
函数中应用中间件验证。通常,如果你想在整个控制器上应用Auth,你需要这样做:
public function __construct()
{
$this->middleware('auth');
}
但是当我删除它,只做:
public function index()
{
$this->middleware('auth');
return view('applicant.index');
}
它不工作。我以前这样做过,效果很好。这是在我的ApplicantController
中,我希望create
功能是公共的,并且只在index
上应用登录身份验证。我不会使用edit, update, delete
你能不能试试
public function __construct()
{
$this->middleware('auth', ['only' => 'index']);
}
你也可以反向操作
public function __construct()
{
$this->middleware('auth', ['except' => ['create', 'store', 'edit', 'update', 'delete']]);
}