I installed ZFCUser
successfully. Now I wonder if there is a way to globally check for authentication.
As outlined in the wiki there are several ways to check for auth. They all work but do I have to put the check-if-clause really in every single action? All my sites should be only accessable when beeing logged in and if not, you should be rerouted to the login page.
Does anybody know if there's a central place where I can put this logic?
You can use ZF2 module BjyAuthorize to block/allow access to pages based on user roles such as
guest
,user
etc usingcontroller guard
,route guard
etcTo be honest, I don't think it is a good idea to block every page for a non-authenticated user. How would you access the login page?
That said, you must know the page being accessed, to make a whitelist of pages accessible for anonymous visitors. To start, I'd suggest to include the login page. You can check pages the easiest by using their route. So check the current matched route against the whitelist. If blocked, act upon. Otherwise, do nothing.
An example would be inside a Module.php from a module, for example your application:
People,
Tip, dont forget the add the "use" to correct RouteMatch statement:
Here need this:
If you forget, the if above have inconstant
Another option might be to create your own abstract controller superclass and implement the onDispatch() method like this:
You can implement a whitelist there too :).
On ZF 2.4.2 I do this in Module.php