In the basic sample application that is used with ZF2, the IndexController is coded as follows:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
There’s just a simple return statement and the contents of index.phtml are automatically included in the output.
The only place I’ve seen the index.phtml referenced is in \module\Application\config\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(
'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',
),
),
I’ve tried commenting this line out:
//'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
But the index.phtml is still included in the output.
So the question is, why is it included? What magic is happening behind the scenes to cause it to still be included?
It seems to me like maybe I’m missing some fundamental automatic mapping handled by ZF2 that I haven’t been able to find in documentation. But if there’s some automatic mapping, why does that line exist in the module.config.php?
So it turns out there’s a redundancy in the
module.config.phpThis line is a static mapping:
These lines are a dynamic mapping:
The Usage Section on this page made it clear to me.
http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html
It uses the Static mapping when it can for performance, and then fails over to the dynamic mapping. Commenting out both actually causes an error, ViewModel() can’t find the page to include.
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/index/index"; resolver could not resolve to a file'