Implementing your own smart 404 handler
In Yii, the error handling is very flexible, so you can create your own error handler for errors of specific types. In this recipe, we will handle a 404 not found error in a smart way: We will show a custom 404 page that will suggest the content based on what was entered in the address bar.
Getting ready
Set up a fresh Yii application by using
yiic webappas described in the official guide.Configure a database and import the following SQL:
CREATE TABLE `article` ( `alias` varchar(255) NOT NULL, `title` varchar(255) NOT NULL, `text` text NOT NULL, PRIMARY KEY (`alias`) );
Generate an
Articlemodel using Gii.Generate an
Articlecrud using Gii.Create the following articles:
Title
Alias
Text
Yii framework
yii-framework
Yii framework is good.
Why Yii
why-yii
Why should I use Yii?
PHP is a good tool
php-is-a-good-tool
PHP is nice!
Yii as a tool for apps
yii-as-a-tool-for-apps
Yii is a good tool for apps.
How to do it...
Carry out the following...