The 404 page
Introduction
As you all (should) know 404 handling is a very important part in the development process. Not only does it show the user that the page he/she/it requested is not available. It also a way to informs machines (browsers and such) about what's going on by profiding a 404 status header.
Configuration
The 404 route is set in app/config/routes.php and must point to the controller/method that handles the 404 pages. Read more about it in the routing section.
404 handling
When a request is made and after the router looked for possible matches and there is no match, the 404 handling comes into play. By default the _404_ route points to welcome/404, let's take a look at that method:
// Inside Controller_Welcome
/**
* The 404 action for the application.
*
* @access public
* @return void
*/
public function action_404()
{
$messages = array('Aw, crap!', 'Bloody Hell!', 'Uh Oh!', 'Nope, not here.', 'Huh?');
$data['title'] = $messages[array_rand($messages)];
// Set a HTTP 404 output header
$this->response->status = 404;
$this->response->body = View::factory('welcome/404', $data);
}
Here you can see what's going on inside the 404 handler. As you can see it's a normal controller action. What's nice about this is that it allowes you to show whatever content you like on the page. You can load your own views with data fetched from a database.
Note that Fuel doesn't set the 404 status, your action must contain $this->response->status = 404; in order to send the correct status header.
Catch all
Because Fuel doesn't set the 404 response status, you can use it as a catch all function. You might have have a page model that can fetch the page from a database by uri. Here is an example to illustrate the possibilities:
// Inside your 404 controller
public function action_my404()
{
$original_uri = \Uri::detect();
$result = \DB::select()->from('pages')->where('uri', $original_uri)->execute();
if(count($result) === 1)
{
// display that page in whatever way you like
}
else
{
// display your general 404 page
$messages = array('Aw, crap!', 'Bloody Hell!', 'Uh Oh!', 'Nope, not here.', 'Huh?');
$data['title'] = $messages[array_rand($messages)];
$this->response->status = 404;
$this->response->body = View::factory('welcome/404', $data);
}
}