Unable to render template - zend-framework2

I am trying to send a form html as a json response. The problem I am having is that ZF2 can not find the forms template file. I get the error
Zend\View\Renderer\PhpRenderer::render: Unable to render template "admin/categories/form.phtml"; resolver could not resolve to a file
In my controller I have
if (!is_null($form)) {
$renderer = new PhpRenderer;
$viewModel = new ViewModel();
$viewModel->form = $form;
$viewModel->category = $category;
$viewModel->setTemplate('admin/categories/form.phtml');
$form = $renderer->render($viewModel);
}
The form.phtml file exists but as I said ZF2 can't find it. In my module_config.php I have
'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(
'admin/layout' => __DIR__ . '/../view/layout/layout.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
'admin' => __DIR__ . '/../view'
),
),
Does anyone know why ZF can not find my template file?
Many thanks in advance.

Try to replace
'template_path_stack' => array(
'admin' => __DIR__ . '/../view'
)
with
'template_path_stack' => array(
'admin' => __DIR__ . '/../view/admin'
)
or add
'template_path_stack' => array(
'admin' => __DIR__ . '/../view',
__DIR__ . '/../view'
)

I finally found out what was happening. The PhpRenderer did not have the paths from the module_config file. I had to get the PhpRenderer from the service manager.
$renderer = $this->getServiceLocator()->get('viewmanager')->getRenderer();
Once I did this the template was located and rendered fine.

Related

Unable to render template "error"; resolver could not resolve to a file ZF2

I have already seen all the links that are already discussed for this topic but could not seem to be working for me, therefor I would like to open this discussion once more and show you my config files as well..
First of all my folder structure is like this:
So far I have module.config.php like this:
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
'view_manager' => array(
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'album/album/index' => __DIR__ . '/../view/album/album/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
)
)
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
);
You probably have an Application module where there is a file view/error/404.phtml. Remove the following lines from the Album module (so not the Application module):
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
What happens is the Album module overrides the Application module with its configuration. The Album module refers to the error page which does not exists in the Album module (see, in the album there is no view/error/404.phtml file).
If you also removed the Application module, get it back from the skeleton application and everything will work again.
you forgot
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
like that:
'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(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
Add this method to Album\Module.php
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

ZF2 module config service_manager add constructor param

I've been searching and trying for a while now and I can't find out what I'm doing wrong...
I've been reading quite some documentation now and have tried a lot of different approaches.
I hope one of you guys can help me out..
My idea:
I have a module called Application. In this module there is an \Application\Controller\IndexController which is used to show the homepage of my application.
The IndexController needs an object with \Zend\http\Client, so I though I should use the factories key in the configuration that is returned by \Application\Module->getServiceConfig() and add a constructor with required param \Zend\Http\Client $client.
Unfortunatly this is not working..
My code:
<?php
// module/Application/Module.php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'IndexController' => function($serviceManager) {
$httpClient = \Zend\Http\Client;
return new IndexController($httpClient);
},
),
);
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
<?php
// module/Application/config/module.config.php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
)
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'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(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
<?php
// module/Application/src/Application/Controller/IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
protected $httpClient;
public function __construct(\Zend\Http\Client $client)
{
$this->httpClient = $client;
}
public function getHttpClient()
{
return $this->httpClient;
}
public function indexAction()
{
$httpClient = $this->getHttpClient();
return new ViewModel();
}
}
The error:
Catchable fatal error: Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Zend\Http\Client, none given, called in /var/www/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined in /var/www/module/Application/src/Application/Controller/IndexController.php on line 13
You need to implement the getControllerConfig method in Module.php to do what you're asking, controllers have their own manager, remove what you have from getServiceConfig, and use the following instead
public function getControllerConfig()
{
return array(
'factories' => array(
'Application\Controller\Index' => function($serviceManager) {
$httpClient = new \Zend\Http\Client;
return new \Application\Controller\IndexController($httpClient);
},
),
);
}
You'll also need to remove the line from controller invokables in module.config.php since it'll conflict with your factory
'controllers' => array(
'invokables' => array(
// this line's no longer needed
//'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),

Zend Framework 2 View Manager - view file and requested action match

I want to make automatic match view file - controller action.
If /Web/TestController/testAction (module/controller/action) requested,
ZF2 tries to load this view : /web/test/test
Then, I have to add this line to template_map to make It work :
'web/test/test' => __DIR__ . '/../view/pages/test/test.phtml',
But I don't want to add this lines for all actions.
Can it work like this :
'web/{ABC}/{XYZ}' => __DIR__ . '/../view/pages/{ABC}/{XYZ}.phtml',
how do i make it so that it would automatically matches ?
module.config.php :
return array(
'router' => array(
'routes' => array(
'web' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action]',
'constraints' => array(),
'defaults' => array(
'controller' => 'Web\Controller\Test',
),
)
)
)
),
'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(
'layout/test' => __DIR__ . '/../view/layouts/test/test.phtml',
'layout/default' => __DIR__ . '/../view/layouts/default/default.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
'web/test/test' => __DIR__ . '/../view/pages/test/test.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'layout' => 'layout/default'
),
);
You should rename the "pages" directory to "web" and change the config file as follows
'web/test/test' => __DIR__ . '/../view/web/test/test.phtml',

zend framework 2 configuring module with dependency injection fails

i'm trying to configure the application module with dependency injection. But the dependencies seem not to be injected. Here is the part of my config:
'di' => array(
'instance' => array(
'Zend\View\Resolver\AggregateResolver' => array(
'injections' => array(
'Zend\View\Resolver\TemplateMapResolver',
'Zend\View\Resolver\TemplatePathStack',
),
),
'Zend\View\Resolver\TemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'home' => __DIR__ . '/../view/home.phtml',
'site/layout' => __DIR__ . '/../view/site/layout.phtml',
'site/error' => __DIR__ . '/../view/site/error.phtml',
'site/404' => __DIR__ . '/../view/site/404.phtml',
),
),
),
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'application' => __DIR__ . '/../view',
),
),
),
'Zend\View\Renderer\PhpRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\Resolver\AggregateResolver',
),
),
'Zend\Mvc\View\DefaultRenderingStrategy' => array(
'parameters' => array(
'layoutTemplate' => 'site/layout',
),
),
'Zend\View\View' => array(
'parameters' => array(
'renderer'=>'Zend\View\Renderer\PhpRenderer'
)
),
)
),
The Zend\View\Renderer\PhpRenderer object is not injected. How can configure my app to use DI for Views and Resolvers?

ZF2 how to change locale in the view

my config is
'translator' => array(
'locale' => 'en_EN',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
and how i can change locale from my view or controller?
Fetch the Translator instance and invoke the setLocale() method. Example (controller context):
// Get the translator from the service locator.
$translator = $this->getServiceLocator()->get('translator');
$translator->setLocale($myLocale);
UPDATE:
$translator = $this->getServiceLocator()->get('translator');
$translator->setLocale($myLocale);

Resources