Skip to content

Tutorial17CustomError

tanakahisateru edited this page Jan 25, 2012 · 2 revisions

Shall you design cool error pages?

When a required resource has not be found error code 404 returned to web browser. At that time response body contains some message describing not to found the resource or such as. These messages which seems Apache7s default are regularly generated by Pinoco, but they are too boring. Customize those error pages more exciting.

To create custom error page you create error directory under application directory, and put a file there named:

[error code].php

Edit it using raw PHP like Tutorial14RawPhp.

Try to customize page design for 404 error.

_app/error/404.php

<div align="center">
<h1>Sorry, we don't have that.</h1>
<p><?php echo $message;?></p>
<p><a href="<?php echo $this->url("/");?>">Go back to home</a></p>
</div>

Here, try to get non-existing HTML. You will see a different page from you've seen, if so your custom design applied successfully.

There are some given variables in custom error page.

$this Pinoco instance
$code Error code
$title Title of error
$message Error detail

Use them to design effective error page.

By the way, to create page files for each errors is troublesome. You can also create default.php for others else.

_app/error/default.php

<div align="center">
<h1><?php echo $code;?> <?php echo $title;?></h1>
<p><?php echo $message;?></p>
<p>Please report to: xxx@xxx.com</p>
<p><a href="<?php echo $this->url("/");?>">Go back to home</a></p>
</div>

All error responses have this design commonly.

To check, raise 500 error in your script.

_app/hooks/500.html.php

<?php
$this->error(500, "Internal server error", "Unexpected system error happened!!");

Move to 500.html in your browser and check how above design would be applied. Let visitors surprised with your beautiful design!

Why not to use PHPTAL to create error pages is because this is the last chance to handle error. To report safer and to be more flexible we decided to use raw PHP there.

Although our standard way is raw PHP but, if you want, you can use PHPTAL renderer explicitly. Pinoco's render() method allows you to show a good looking page generated by template engine.

_app/error/default.php

<?php
$this->autolocal->title = $title;
$this->autolocal->message = $message;
$this->render("/_error/" . $code . ".html");