Not locking every method in controller under auth middleware

Sometimes it is not required to put every controller method under auth middleware. We can achieve the result by following snippet.

class CommunityTopicController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth')->except(['show']);
    }
}	


If we have controller CommunityTopic and only one method of it such as show needs not to be under auth middleware from the rest of the methods such as create, store, index, edit, update and destroy we can use above code snippet to achieve the result.

In this case, users of the site ( who are not logged in ) will be able to visit show page only.

Related Posts