Search code examples
phpzend-framework2

ZF2 phprenderer & routing


I am getting

Zend\View\Renderer\PhpRenderer::render: Unable to render template "link- 
account-manager/add-accounts/index"; resolver could not resolve to a file

and I'm not sure why phprender is looking in link-account-manager when I want it to be looking in linkaccount-manager. Is this a default behavior? I tried recursively searching for "link-account-manager" and there were no matches.


Solution

  • It is indeed ZF2 default behavior. You may have a look at \Zend\View\Renderer\PhpRenderer and \Zend\View\Resolver\*.

    The template resolver transforms a camel case Module or Controller (LinkAccountManager, AddAccountsController) names into a template path like link-account-manager\add-accounts\index.phtml.

    It is possible to configure some values in the module.config.php:

    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'your-custom-template' => __DIR__ . '/../view/your-custom-template.phtml' // e.g. !!
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
            // add custom paths here
        ),
        ...
    ),
    

    Further, you can also change the template via view models. Assuming you have your folder structure like 'linkaccount-manager\view\linkaccountmanager\' (in the default module's view folder!), e.g.:

    namespace LinkAccountManager;
    
    public class AddAccountsController extends AbstractActionController
    {
        public function indexAction()
        {
            $viewModel = new ViewModel();
            ...
            $viewModel->setTemplate('linkaccount-manager\add-accounts\index');
            return $viewModel;
        }
    }