Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auth on specific controller functions Laravel 5.1

I am working on a Laravel 5.1 based system. I have a route resource:

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

So as we expect it has the following functions in the controller:

index, create, store, edit, update, delete

And what I want is to apply the middleware auth in the index function only. Normally, if you want to apply Auth on the entire controller, you need to do:

public function __construct()
{
    $this->middleware('auth');
}

But when I remove it and just do:

public function index()
{
    $this->middleware('auth');
    return view('applicant.index');
}

It doesn't work. I've done this before and works fine. This is in my ApplicantController I want the create function to be public and only apply login authentication on index. I won't be using edit, update, delete

like image 329
jackhammer013 Avatar asked Sep 14 '15 13:09

jackhammer013


1 Answers

can you try

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

You can also do the reverse

public function __construct()
{
    $this->middleware('auth', ['except' => ['create', 'store', 'edit', 'update', 'delete']]);
}
like image 154
codegenin Avatar answered Oct 07 '22 14:10

codegenin