header image

Krishnan's Personal Website


Home | Resume | Blog | Interesting Reads | Tutorials | Skills | Personal Projects | Books | Newsletters | Certifications | Fun | Connect with me


Custom 404 page in Phalcon


Published On: Jul 31 2021
Written By: Krishnan Sethuraman
Category: PHP


Phalcon is a PHP web framework that is superfast and can be used to build both micro and full stack web applications.

I have been using Codeigniter and some Laravel all this while but decided to give Phalcon a try. The first project was to migrate this website from Codeigniter to Phalcon.

Migrating the static pages was not very difficult. If you have experience working with either Laravel or Codeigniter then this would not be difficult.

The main confusion arose when I was creating the controllers and methods. Unlike Codeigniter, Phalcon has automatic route enabled. So the URLs work like a charm without creating any routes.

However you will encounter an obstacle if you want to set up a custom 404 page. With little bit of research I eventually figured out that this was not very difficult.

Phalcon had a notFound option in routes to set up custom 404 pages. However this notFound does not work unless the auto route is turned off.

To turn off auto route you need to navigate to config/route.php and

$router = $di->getRouter();
to
$router = $di->getRouter(false);

Once done you can now add the below code to route.php and have a custom 404 page.

$router->notFound( [ 'controller' => 'error', 'action' => 'notFound', ]);

However the only drawback to this approach is that you will have to create all other routes that you need in route.php as the auto route is turned off.