简体   繁体   中英

Zf2 layout and authentication

I am building a ZF2 based site, and it has 2 different layouts. It has some public pages and some internal pages which require user authentication, each set of pages have their layouts. I was initially thinking of creating a controller plugin and check if the user has identity then I will change the layout, but it would also change the layout of public pages for a logged in user, which we don't want. Also I don't know how to change the layout from a controller plugin.

The layout of internal pages also require some information regarding the current users and some other information from other tables. So I was thinking of creating another plugin which will check if the user has identity and thn it will set the layout variables. I am not sure how to get the layout object in the controller plugin to set variables. Also don't know how to get one plugin access other plugin.

Also for the internal pages I am checking in every action if the user is logged in or not. If he has no identity, I am sending him to login page. This resulted in lots of repeating code, so is there a way to move this code to a controller plugin, and some-how tell it to check user's identity on specific pages and if user has identity let him access the page otherwise send him to login screen.

Ultimately this looks like a custom LayoutModule that you'd write. I assume you've already taken a look at EdpModuleLayouts ? If not, do so.

What this module basically does is: it checks the current Module and then checks if there is a Layout attached within Configuration for this very Module.

You ultimately need just that, with the only difference being that you don't attach a Layout to a whole Module, but probably rather to specific Routes. You would check what the current Route is and if it is a Route that has a Layout attached in your Configuration , you'd change the layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM