zf3在操作中获得不同的视图 [英] zf3 getting different view in an action

查看:82
本文介绍了zf3在操作中获得不同的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在zf2中,我正在这样做,

in zf2 i was doing this,

$view = 'application/use/view';
$htmlString = $this->getServiceLocator()
        ->get('viewmanager')
        ->getRenderer()
        ->render(
            $view,
            array(
                'institute' => $institute,
                'gender' => $gender
            )
        );

但是zf3中没有直接的getServiceLocator方法,如何通过工厂在zf3中执行此操作 到目前为止,我已经做到了:

but there is no direct getServiceLocator method in zf3 how can i do this in zf3,via factory So far i have done this:

在我的工厂

public function __invoke(ContainerInterface $container, $requestedName, Array $options = null) {
    $auth = $container->get('doctrine.authenticationservice.orm_default');
    $view = $container->get('Zend\View\Renderer\PhpRenderer');
    $entityManager = $container->get('doctrine.entitymanager.orm_default');
    return new $requestedName($entityManager, $auth, $view);
}

以及我该怎么做

推荐答案

您使用Google吗? 控制器"

Did you Google it? First hit for "zf3 set view in controller"

像这样实现它:

在配置中注册新模板或任何您想要的模板.

Register your new template, or any you wish, in your config.

'view_manager' => [
    'template_map' => [
        'layout/layout' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR .
            'layout' . DIRECTORY_SEPARATOR . 'layout.phtml',
        'layout/second-layout' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR .
            'layout' . DIRECTORY_SEPARATOR . 'second-layout.phtml',
    ],
    'template_path_stack' => [
        __DIR__ . DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . 'view',
    ],
],

在操作中应用模板

public function indexAction() 
{    
    // Use a different view template for rendering the page.
    $viewModel = new ViewModel();
    $viewModel->setTemplate('layout/second-layout');
    return $viewModel;
}

这篇关于zf3在操作中获得不同的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆