Handling 404 and 500 error pages in the Phalcon PHP Framework

February 16th 2014

I'm finally doing it. Phalcon's mature enough for me to look at running a small site with it. The performance is already hitting around 5,500 requests per second in my development environment.

Anyway, one of the things I was trying to figure out was how to handle the 404 (Not Found) and 500 (Internal Server Error) within the application.

Needless to say, I figured it out, but to be perfectly honest, it wasn't that bad.

Everything in Phalcon is event driven, and we rely heavily on the Dependency Injection service to feed key pieces of information into our application.

There's a service you can use which basically you can override key parts of the event management system. You'll want to hook into the 'dispatch:beforeException' event, and tell the application to do something else.

In the example below (from my bootstrap file) I essentially tell Phalcon that in the case of:

Text Snippet:
\Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND  - code 2

or

Text Snippet:
\Phalcon\Mvc\Dispatcher::Dispatcher::EXCEPTION_ACTION_NOT_FOUND - code 5

Execute my 'ErrorController::notFoundAction'

And for any other type of Exception, execute the 'ErrorController::uncaughtExceptionAction'

PHP Code:
<?php
use \Phalcon\Mvc\Dispatcher;

$di = new FactoryDefault();

// some of your di services here

$di->set(
    
'dispatcher',
    function() use (
$di) {
        
$eventsManager $di->getShared('eventsManager');
        
$eventsManager->attach(
            
'dispatch:beforeException',
            function(
$event$dispatcher$exception) {
                switch (
$exception->getCode()) {
                    case 
Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                    case 
Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                        
$dispatcher->forward(
                            array(
                                
'controller' => 'error',
                                
'action' => 'notFound',
                            )
                        );
                        return 
false;
                        break; 
// for checkstyle
                    
default:
                        
$dispatcher->forward(
                            array(
                                
'controller' => 'error',
                                
'action' => 'uncaughtException',
                            )
                        );
                        return 
false;
                        break; 
// for checkstyle
                
}
            }
        );
        
$dispatcher = new Dispatcher();
        
$dispatcher->setEventsManager($eventsManager);
        return 
$dispatcher;
    },
    
true
);

It's not that terrible, and to ensure completeness of this blog post, here's my ErrorController:

PHP Code:
<?php
class ErrorController extends \Phalcon\Mvc\Controller
{
    public function 
notFoundAction()
    {
        
// The response is already populated with a 404 Not Found header.
    
}

    public function 
uncaughtExceptionAction()
    {
        
// You need to specify the response header, as it's not automatically set here.
        
$this->response->setStatusCode(500'Internal Server Error');
    }
}

If you want views, you'll need to create the files in your views folder. The format should be:

Text Snippet:
views/error/notFound.phtml
views/error/uncaughtException.phtml

As you've probably figured out, this blog is my place to hold sweet pieces of code that I just don't want to rewrite, but hopefully you'll find it useful as well.

Fleta commented on Jul 19th 2016

At last! Someone with real exsretipe gives us the answer. Thanks!