zf3 getting different view in an action

佐手、 提交于 2019-12-08 15:22:29

问题


in zf2 i was doing this,

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

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

in my factory

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);
}

and in my action how do i do this


回答1:


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

Implement it like so:

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',
    ],
],

Applying a template in an action

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


来源:https://stackoverflow.com/questions/49389079/zf3-getting-different-view-in-an-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!