404 and 500 errors in Zend Framework

First of all, I want to review the several mechanisms that Zend Framework provide to handle exceptions in it’s MVC components.

 

1. Pass the exceptions to Zend_Controller_Plugin_ErrorHandler.

 

2. Using Zend_Controller_Front::throwExceptions(true) :

  

[codesyntax lang=”php”]

$front->throwExceptions(true);

try {
    $front->dispatch();
} catch (Exception $e) {
    // handle exceptions yourself
}

[/codesyntax]

 

3. Using Zend_Controller_Response_Abstract::renderExceptions(true). But it’s not the common way and recommended for non-production environment.

 

4. Using Zend_Controller_Front::returnResponse(true) :

 

[codesyntax lang=”php”]

$front->returnResponse(true);
$response = $front->dispatch();

if ($response->isException()) {
    $exceptions = $response->getException();
    // handle exceptions ...
} else {
    $response->sendHeaders();
    $response->outputBody();
}

[/codesyntax]

 

See more info in manual :

http://framework.zend.com/manual/en/zend.controller.exceptions.html#zend.controller.exceptions.handling

 

Here we will mainly take a look into the 4th mechanism which is considerd to be the best from official.

 

Let’s make some configurations to Zend_Controller_Front :

 

[codesyntax lang=”php”]

$front = Zend_Controller_Front::getInstance();
$front->setParam('noErrorHandler', true)
      ->throwExceptions(false)
      ->returnResponse(true);

[/codesyntax]

 

We have just canceled the error handler plugin and forbade to throw exceptions and store the exceptions within the response :

 

 

[codesyntax lang=”php”]

$response = $front->dispatch();

if ($response->isException()) {
    $exceptions = $response->getException();
    echo handleException($exceptions[0]);
} else {
    echo $response;
}

function handleException(Exception $e)
{
    switch ($e->getCode()) {
        case 500: {
            if (!include_once(PROJECT_ROOT . '/error/500.html')) {
                @header("HTTP/1.x 500 Internal Server Error");
                @header('Status: 500 Internal Server Error');
            }
            exit;
            break;
        }

        default: {
            if (!include_once(PROJECT_ROOT . '/error/404.html')) {
                @header('HTTP/1.x 404 Not Found');
                @header('Status: 404 Not Found');
            }
            exit;
            break;
}

[/codesyntax]

 

At last we write the content of 404.html and 500.html. I will show you how to make the SEO to 404.html for example :

 

[codesyntax lang=”html4strict”]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>kimbs.info - 404 error: Page not found</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="kimbs.info" />
<meta name="keywords" content="kimbs.info" />
<meta name="robots" content="*" />
<style type="text/css">
.STYLE1 {
    color:#0000FF; 
    font-weight:bold; 
    font-size:25px
    font-weight: bold;
}

.STYLE2{
    font-size:15px;
    line-height:25px
}
</style>
</head>
<body style="margin-top:100px;">
    <div style="margin:0 auto; width:650px;">
        <p>
            <h3 class="STYLE1">
                Sorry, the page you are trying to access
                does not exist or perhaps it was removed.
            </h3>
        </p>

        <br />

        <p>
            <h2 class="STYLE2">
                1. Please check your input
            </h2>
        </p>

        <p>
            <h2 class="STYLE2">
                2. Welcome back to home page <a href="http://kimbs.info/">http://kimbs.info/</a>
            </h2>
        </p>

        <p>
            <h2 class="STYLE2">
                3. Thank you for visiting and
                <a href="http://kimbs.info/contract">contract me</a> if any question
            </h2>
        </p>
    </div>
</body>
</html>

[/codesyntax]

 

Posted in Zend Framework | Tagged , | Leave a comment