Kohana 404 custom page-Collection of common programming errors

here is how I do it with Kohana 3.2

  • Add exceptions handling stuff in index.php
    try
    {
        $request = $request->execute();
    }
    catch(Kohana_HTTP_Exception_404 $e)
    {
        $request = Request::factory('errors/404')->execute();
    }
    catch(Exception $e)
    {
        $request = Request::factory('errors/500')->execute();
    }

    echo $request->send_headers()->body();
  • Then write Errors controller
class Controller_Errors extends Controller
{
    public function __construct($request, $response)
    {
        parent::__construct($request, $response);
    }

    public function action_404()
    {
        $this->response->body(View::factory('errors/404'));
    }

    public function action_500()
    {
        $this->response->body(View::factory('errors/500'));
    }
}
  • Create 2 corresponding error pages (404.php and 500.php in views/errors)

  • Add new route to your bootstrap.php or use default one (depends on you project’s structure), just make sure Controller_Errors can be reached when exception is thrown

  • Now every time you throws the exception in your controller, it will display the custom error page, like this
throw new HTTP_Exception_404;

Originally posted 2013-11-27 12:00:40.