gpt4 book ai didi

php - 对特定 Controller 功能的授权 Laravel 5.1

转载 作者:可可西里 更新时间:2023-11-01 14:01:20 24 4
gpt4 key购买 nike

我正在开发基于 Laravel 5.1 的系统。我有一个路线资源:

Route::resource('applicant', 'ApplicantController');

所以正如我们所期望的那样,它在 Controller 中具有以下功能:

index, create, store, edit, update, delete

我想要的是仅在 index 函数中应用中间件身份验证。通常,如果你想在整个 Controller 上应用 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']]);
}

关于php - 对特定 Controller 功能的授权 Laravel 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com