user16948
user16948

Reputation: 4951

Zend framework 2 error: unable to render template

I'm working on this tutorial. I have followed all steps to create directory structure, controllers, module.php and module.config.php, but when I open http://zf2-tutorial/album I get the following error:

 Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file' in /var/www/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/View/Renderer/PhpRenderer.php:461

I have created a file named index.phtml inside module directory in view/album/album directory.

module.config.php:

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

);

What's wrong?

Upvotes: 0

Views: 30178

Answers (12)

Marashlyan Suren
Marashlyan Suren

Reputation: 1

check your application.config.php module name if it is 'Album' not 'album' change

'template_path_stack' => array(
    'album' => __DIR__ . '/../view', => //to 'Album' => __DIR__ . '/../view',
),

Upvotes: 0

Bhavik Joshi
Bhavik Joshi

Reputation: 2677

You should return a view object in IndexController like

$view = new ViewModel(array('albums' => $this->getAlbumTable()->fetchAll(),));
$view->setTemplate("album/album/index/index.phtml");
return $view;

instead of directly returning

return array('albums' => $this->getAlbumTable()->fetchAll(),);

Upvotes: 1

user3477784
user3477784

Reputation: 25

Had the same problem, i didn't create empty view files module/Album/view/album/album/index.phtml module/Album/view/album/album/add.phtml module/Album/view/album/album/edit.phtml module/Album/view/album/album/delete.phtml

Upvotes: 0

Mickael
Mickael

Reputation: 1

In my case, the trouble was in the view file name. I named it index.php instead of index.phtml.

Upvotes: 0

George Otieno
George Otieno

Reputation: 546

Ensure that you use forward slash and not backslash when in the template_map array which is inside the view_manager of the module.config.php

Upvotes: 0

vundek
vundek

Reputation: 1

There isn't 'home' route anymore in Album\config\module.config.php so

it's necessary to replace 'H/home' to 'A/album' in ..\module\Album\view\layout\layout.phtml

Upvotes: 0

Amernath
Amernath

Reputation: 358

I faced the same problem. I've changed the below line of file module.config.php, Existing in the document,

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

to

'view_manager' => array(
  'template_path_stack' => array(
  'album' => __DIR__ . '/../src/view',
  ),
),

Just add src folder name in front of view folder.

Like this, you need to add 'src' folder name before all the view paths.

Upvotes: 2

kamlesh.bar
kamlesh.bar

Reputation: 1804

Apart from this you need to add 2 things in module.config.php in routes array put below code

'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
        ),

and translator array along with language folder

'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

Upvotes: 0

George Otieno
George Otieno

Reputation: 546

check if you have provided the correct parameters or nesting for your view_manager in the module.config.php file

Upvotes: 4

michau
michau

Reputation: 1

Your view_manager is in wrong place. It should be on "controllers" and "router" level, not inside "controllers". Look at module.config.php in Application module and see the difference.

Upvotes: 0

2plus
2plus

Reputation: 261

I have checked your codes with Album module. Everything is fine. You may have a problem with htaccess file. Please check.

Upvotes: 0

DrBeza
DrBeza

Reputation: 2251

The PhpRenderer object is complaining because it cannot find the view phtml file.

Does the file module/Album/view/album/album/index.phtml exist? If not then that's likely the cause.

Upvotes: 6

Related Questions