在特定控制器功能Laravel 5.1上进行验证

jac*_*013 5 php authentication laravel laravel-5.1

我正在研究基于Laravel 5.1的系统.我有一个路线资源:

Route::resource('applicant', 'ApplicantController');
Run Code Online (Sandbox Code Playgroud)

因此我们期望它在控制器中具有以下功能:

index, create, store, edit, update, delete
Run Code Online (Sandbox Code Playgroud)

我想要的只是在index函数中应用中间件auth .通常,如果要在整个控制器上应用Auth,则需要执行以下操作:

public function __construct()
{
    $this->middleware('auth');
}
Run Code Online (Sandbox Code Playgroud)

但当我删除它时,只是做:

public function index()
{
    $this->middleware('auth');
    return view('applicant.index');
}
Run Code Online (Sandbox Code Playgroud)

它不起作用.我以前做过这个并且工作正常.这是在我的ApplicantController我希望该create功能是公开的,只应用登录身份验证index.我不会用edit, update, delete

小智 6

你能试一下吗

public function __construct()
{
    $this->middleware('auth', ['only' => 'index']);
}
Run Code Online (Sandbox Code Playgroud)

你也可以反过来

public function __construct()
{
    $this->middleware('auth', ['except' => ['create', 'store', 'edit', 'update', 'delete']]);
}
Run Code Online (Sandbox Code Playgroud)