Tag Archives: zf2

How to find out the real cause of the boring RuntimeException in ZF2

Nowadays I’m just getting annoying error stack traces from zend framework 2 with a message like:

PHP Fatal error:  Uncaught exception 'ZendViewExceptionRuntimeException' with message 'ZendViewRendererPhpRenderer::render: Unable to render template "error"; resolver could not resolve to a file' in C:worksrcvendorzendframeworkzendframeworklibraryZendViewRendererPhpRenderer.php:451

and naturally the stack trace doesn’t even shows where the real problem happens.

I tried to put the problematic code lines in a try-catch block, but the same happened: uncaught exception. That was really weird, because I tried to catch Exception, which should be every exception’s parent class. Later I found out that since the current file is in a specific namespace, then I have to specify the exact class type of the Exception class. For example:

<?php
namespace SomeName;
class Whatever
{
    try
    {
        //some erroneous code
    }
    catch (Exception $e)
    {
        echo $e;
    }

}

If you would try to catch Exception, then the exception class should be SomeNameException, so that’s why you have to use it like Exception, so php will try to find the class in the root namespace.

 

Setting the doctype in Zend Framework 2

I recently had to change the doctype in a Zend Framework 2 environment. It was kind of difficult to find the answer so I share with you. Simply set the doctype before “running it” in index.php like this:

<?php
/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir(dirname(__DIR__));

// Setup autoloading
include 'init_autoloader.php';

$doctypeHelper = new ZendViewHelperDoctype();
$doctypeHelper->setDoctype($doctypeHelper::HTML5);

// Run the application!
ZendMvcApplication::init(include 'config/application.config.php')->run();

The possible doctypes are:

XHTML11            
XHTML1_STRICT      
XHTML1_TRANSITIONAL
XHTML1_FRAMESET    
XHTML1_RDFA        
XHTML1_RDFA11      
XHTML_BASIC1       
XHTML5             
HTML4_STRICT       
HTML4_LOOSE        
HTML4_FRAMESET     
HTML5              
CUSTOM_XHTML       
CUSTOM

That’s all folks.