问题
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