Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Setting custom layout with ControllerFactory #81

Closed
armenio opened this issue Jul 11, 2016 · 2 comments · Fixed by #187
Closed

Setting custom layout with ControllerFactory #81

armenio opened this issue Jul 11, 2016 · 2 comments · Fixed by #187

Comments

@armenio
Copy link

armenio commented Jul 11, 2016

With the suggested code by @weierophinney:

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $plugins = $container->get('ControllerPluginManager');

        $layout = $plugins->get('layout')->setTemplate('layout/admin');

        return (null === $options) ? new $requestedName : new $requestedName($options);
    }

shows this Exception:
Layout plugin requires a controller that implements InjectApplicationEventInterface

So, I did a try with this code:

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $plugins = $container->get('ControllerPluginManager');

        $controller = (null === $options) ? new $requestedName : new $requestedName($options);
        $layout = $plugins->get('layout');
        $layout->setController($controller);
        $layout->setTemplate('layout/admin');

        //return (null === $options) ? new $requestedName : new $requestedName($options);
        return $controller;
    }

The Exception does not appear, but the code had no effect

@froschdesign
Copy link
Member

froschdesign commented Sep 14, 2017

A listener, which listen on Zend\Mvc\MvcEvent::EVENT_RENDER is a better choice.

Example:

public function setLayout(MvcEvent $event)
{
    $routeMatch = $event->getRouteMatch();

    if ($routeMatch
        && 0 === strpos(
            $routeMatch->getParam('controller'), 'Admin'
        )
    ) {
        $layoutViewModel = $event->getViewModel();
        $layoutViewModel->setTemplate('layout/admin');
    }
}

@froschdesign
Copy link
Member

Should be added to documentation as cookbook recipe.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants