Apply Middleware for certain methods
Hi guys,
I got a bit issue with using middleware in L5.
I have a resource called 'Post' like below
Route::resource(
'post', 'PostController'
);
Now I wanna apply middle 'auth' on create,store,edit and delete method and let user access to index, show without logging in.
Not sure how to do that.
Thank you in advance.
You can add middleware for certain routes in the constructor of the controller like this:
/**
* Enforce middleware.
*/
public function __construct()
{
$this->middleware('auth', ['only' => ['create', 'store', 'edit', 'delete']]);
// Alternativly
$this->middleware('auth', ['except' => ['index', 'show']]);
}
Aha, That works !!!!. Couldn't see this syntax in the documentation.
I'm happy to help. Here is the documentation by the way, I agree it could be clearer.
Please or to participate in this conversation.