Zf2改变布局

时间:2014-10-02 10:17:44

标签: php zend-framework2 zend-layout

我正在尝试根据我的路线更改布局,这是我到目前为止所做的事情:

<?php namespace Application\Listener;

use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\ModelInterface as Model;

class LayoutListener extends AbstractListenerAggregate
{
    /**
     * Attach one or more listeners
     * Implementors may add an optional $priority argument; the EventManager
     * implementation will pass this to the aggregate.
     * @param EventManagerInterface $events
     * @return void
     */
    public function attach(EventManagerInterface $events)
    {
        $sharedEvents = $events->getSharedManager();
        $this->listeners[] = $sharedEvents->attach(
            'Zend\Mvc\Controller\AbstractController',
            MvcEvent::EVENT_DISPATCH,
            array($this, 'handleLayouts'),
            -99);
    }

    public function handleLayouts(MvcEvent $event)
    {
        $viewModel = $event->getViewModel();
        if (!$viewModel instanceof Model) {
            return;
        }

        $routeMatch = $event->getRouteMatch();
        $resolver = $event->getApplication()
            ->getServiceManager()
            ->get('Zend\View\Resolver\TemplatePathStack');

        if ('Application\Controller\Index' !== $routeMatch->getParam('controller')) {
            return;
        }

        switch ($routeMatch->getParam('action')) {
            case 'index':
                $template = 'layout/layout';
                break;
            case 'home':
                $template = 'layout/home';
                break;
            default:
                $template = null;
                break;
        }

        /**
         *
         * Don't know why, but to set a template must use $event->getViewModel()
         * and for terminal must use $event->getResult(), otherwise it won't works
         *
         */

        // if template is resolvable, use it, otherwise go terminal
        if ($template && $resolver->resolve($template)) {
            $viewModel->setTemplate($template);
        } else {
            if ($event->getResult() instanceof Model) {
                $event->getResult()->setTerminal(true);
                $viewModel->setTerminal(true);
            }
        }
    }
}

Module.php

public function onBootstrap(MvcEvent $e)
{
    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        ->setFallbackLocale('en_US');

    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attachAggregate(new LayoutListener());

    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
}

有一件事我没有得到,如果我想改变布局,我必须使用

  • $event->getViewModel()->setTemplate($template);

否则它将无法工作,如果我想设置viewModel终端,我必须使用

  • $event->getResult()->setTerminal(true);

否则它将无效,有人可以解释我为什么?

我正在努力实现它的目标如下:我有一个索引模板/初始化加载任何其他路径内容的iFrame,所以在索引布局中我设置了菜单,任何其他页面都没有需要有一个布局,因为使用索引/一个。

现在我想要一个具有自定义布局(自定义显示)的子页面,因此,根据路径,我为viewmodel设置了一个新的布局。

1 个答案:

答案 0 :(得分:1)

您可以将子视图添加到视图模型中(如果您在Controller中) - 如果您为子视图(或子页面)创建自己的控制器:

$view = new ViewModel(array(
    // parameters
));

$view->addChild($this->forward()->dispatch('SubpageController', array(
    'action' => 'index',
)), 'contentOfSubpage');

return $view;

在模板中,您可以像这样访问SubpageController的内容:

<?php echo $contentOfSubpage; ?>

至少,这就是如何组合多个控制器,因此不同的布局