ZF2 "Unable to display template" Zend looks in the wrong directory

I have a strange situation ...

After creating the ZF2 SkeletonApplication, I created an additional module called Authentication with AuthController and LoginAction, also in the browsing directory "authentication / auth" I placed login.phtml.

When I launch the application, I get an error

Zend\View\Renderer\PhpRenderer::render: Unable to render template "authentication/auth/login"; resolver could not resolve to a file 

It is strange that when I put the full folder “authentication / auth / login.phtml” in the view folder of the standard application module, it finds it.

So, Zend is looking for the wrong directory.

This is my module.config.php (authentication module).

 return array( 'router' => array( 'routes' => array( 'authentication' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/authentication/login', 'defaults' => array( 'controller' => 'Authentication\Controller\Auth', 'action' => 'login', ), ), ), ), ), 'controllers' => array( 'invokables' => array( 'Authentication\Controller\Auth' => 'Authentication\Controller\AuthController', ), ), 'viewmanager' => array( 'template_path_stack' => array( 'authentication' => __DIR__ . '/../view', ), ) ); 

This is AuthController

 namespace Authentication\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class AuthController extends AbstractActionController { public function loginAction() { return new ViewModel(); } } 

Hope someone can point me in the right direction.

+4
source share
2 answers

The full path cannot be resolved by zf2. ViewManager uses the pathStack template to search for a relative view. In your example, viewManager looks for this file: DIR . "/../view/authentication/authentication/login.phtml

Otherwise, you can add a MapMap to your viewManager as follows:

 'view_manager' => array( 'template_map' => array( 'authentication/auth/login' => __DIR__ . '/../view/where/you/want.phtml', ) ); 
+4
source

change the configuration:

 'template_path_stack' => array( 'authentication' => __DIR__ . '/../view', ), 

I guess you are too far back.

If you are here:

 Authentication/config/module.config.php 

then you want to return to only one level, and then to your browsing directory. Your code will take you back one level to the module directory.

+2
source

Source: https://habr.com/ru/post/1493669/


All Articles