2

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.

M Hill
  • 225
  • 2
  • 6

1 Answers1

1

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.

Sam
  • 16,435
  • 6
  • 55
  • 89
  • thanks Sam, that solves one problem. 2ndly i need to set some user info into layout variables for the 2nd layout, so any suggestions. – M Hill Sep 26 '13 at 07:58
  • ViewHelpers - A VH that will gain access to the UserIdentity and display whatever you like. – Sam Sep 26 '13 at 08:19
  • but the information I need to display isn't part of the user entity. actually its a summary of the application, which is retrieved from atleast 3 places. – M Hill Sep 26 '13 at 08:20
  • [this may help](http://www.michaelgallego.fr/blog/2012/10/06/how-to-replace-the-action-helper-in-zf-2-and-make-great-widgetized-content/) – Sam Sep 26 '13 at 08:57
  • thanks Sam, just a small thing now. I will be using the ViewHelper. So I can either fetch all the data within the ViewHelperFactory and pass the data to the ViewHelper, or I can pass the services to the ViewHelper along with user_id(which is required to fetch the data). So which approach you think should be used. – M Hill Sep 26 '13 at 09:33
  • Whatever you prefer. Best Practice is for ViewHelper to be "easy" and not bloated. But passing a specific Service into the ViewHelper is no problem. As long as you don't pass the ServiceLocator, it's all good ;) – Sam Sep 26 '13 at 11:03