Search code examples
zend-framework2zend-aclzend-layout

ZF2 User group specific layouts


I have a project with 3 different Layouts. 1 Layout for the Login page, 1 Layout for the administrators and editors and a 3rd Layout for general users where also the administrators have access to (its a layout for 3 different pages where you can fill out surveys which is possible for the 3 mentioned user groups).

At the moment I am using EdpModuleLayouts for this purpose, which did fine until now. Because now I need to adapt the 3rd layout depending on which usergroup accesses it.

Do you have any idea how to get this done?

Thanks in advance.


Solution

  • you can listen you to the zf2 mvc events and change your layout based on your criterias. this is an example how your application Module.php can look like.

    public function onBootstrap(MvcEvent $e)
    {
        $application =  $e->getApplication();
        $serviceManager = $application->getServiceManager();
        $eventManager = $application->getEventManager();
        $sharedManager = $eventManager->getSharedManager();
    
        // DISPATCH EVENT
        $sharedManager->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function( MvcEvent $e) use ($serviceManager) {
    
            // get your instance to locate current user group or something else
            $auth = $serviceManager->get('Some/Auth/Service');
            $userGroup = $auth->getUserGroupFromCurrentUser();
    
            $controller      = $e->getTarget();
    
            if( $userGroup == 'someGroup' )
            {
                $controller->layout('layout/somelayoutname');
            } else {
                $controller->layout('layout/someotherlayoutname');
            }
        }, 50 );
    }