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
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']]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With