Support the ongoing development of Laravel.io →
Authentication Validation Architecture
Last updated 2 years ago.
0

The 'before' filter runs before the request is completed. And the 'after' filter runs after the request is completed.... But how does it work? When someone or something makes a request to your system, laravel looks up the route, if the route points to a controller method and there is a 'before' filter, then the code for the filter runs before the controller code. The same thing if the route points to a page, and there is a 'before' filter, the filter code will run before the page is rendered.

The 'before' filter code gives you the chance to test conditions and reroute the request if needed, or allows you to set variables for the view, or whatever you may need to happen before any views or controllers are touched.

The best example would be a page that you require someone to be logged in in order to view it. You would add a 'before' filter to the route. Then code the filter to check if the user is logged in, if yes the filter does nothing and the request is completed. If the user isn't logged in the filter redirects the user to the login page. This way no matter what a user does, if they hit the route and aren't logged in they will always be redirected to the login page, the controller code and views are never touched and your content is protected.

Does that help?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

apache123 apache123 Joined 3 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.