YII how to handle custom 404 error page along with other error pages

22,243

Solution 1

Use this code for actionError:

$app = Yii::app();
if( $error = $app->errorHandler->error->code )
{
    if( $app->request->isAjaxRequest )
        echo $error['message'];
    else
        $this->render( 'error' . ( $this->getViewFile( 'error' . $error ) ? $error : '' ), $error );
}

In views/site create error404.php for 404 errors and error.php for the rest.

Or you can define param in the config for errors you like to handle differently and check error code against it:

$app = Yii::app();
if( $error = $app->errorHandler->error->code )
{
    if( Yii::app()->request->isAjaxRequest )
        echo $error['message'];
    else    
        $this->render( 'error' . ( in_array( $error, $app->params[ 'customErrorPages' ] ) ? $error : '' ), $error );
}

Error handler works like this: when httpexception arise, component will check if there is any value in errorAction property and if there is any, will run this controller's action. If there is no set value to the errorAction property, will display error view from system folder. So there is no need to mix error views from system view folder and controller's view folder.

Solution 2

Whenever errors occur, the action error in siteController is called. you can customize the error route in that action, you can do something like this:

if(404==Yii::app()->errorHandler->error->code){
     //go to custome error page
else
   //code default error.php

Solution 3

Can't you do this with .htaccess? Personally I create an "errors" folder with all the html php files that holds the error messages and modify .htaccess to call those pages while coming across the error.

Links:

http://www.javascriptkit.com/howto/htaccess2.shtml

http://www.addedbytes.com/for-beginners/error-documents-for-beginners/

Examples:

Create a .htaccess file in the directory you want the error pages to be called and in the text file, write the following line:

ErrorDocument 404     /404.html

assuming there is a page called 404.html in the same directory, when a 404 page not found error is genrated, the 404.html page will be called.

The same works with other error codes:

ErrorDocument 500     /500error.html

assuming a 500 error was created and a 500error.html file exists in the same directory.

Solution 4

In the latest versions of the framework (I am working with 1.14) use:

Yii::app()->errorHandler->error['code']

because error is an array.

Share:
22,243
wolvorinePk
Author by

wolvorinePk

Software Engineer , Software Architect , Software Research Engineer ...

Updated on July 09, 2022

Comments

  • wolvorinePk
    wolvorinePk almost 2 years

    I want to display 404 error page for that i have made error404.php file in my protected/view/system folder.

    By default i have Sitecontroller and it contained error action function as below

    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
    
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }
    

    inside main config file it is defined as

        'errorHandler'=>array(
            // use 'site/error' action to display errors
            'errorAction'=>'site/error',
        ),
    

    my problem is i need to customize 404 page only, rest of the error i need to handle the way it is being handle by sitecontroller's error function. But i could not find a way to do that. If suppose i remove 'errorAction'=>'site/error', from the config main then it does show the 404 error by calling

            throw new CHttpException(404, 'Page not found');
    

    but doing that i can only see the page without layout also other custom errors are treated same as 404 while they are not. I read the manual many times but i still cant able to resolve it.

  • wolvorinePk
    wolvorinePk over 11 years
    you mean providing if then else condition inside error view and then add partial views accordingly? (I Thought it should be an automatic if i am using error404.php inside the system directory.???
  • bingjie2680
    bingjie2680 over 11 years
    yes..I am not sure it works with automatic...see the updates.
  • wolvorinePk
    wolvorinePk over 11 years
    i know ho to do that using htaccess but i am learning YII and my system required that i must use it through the yii. Kindly if you have any idea how to do that using yii because even its tutorial is very vague and does not give broader view at all. Help me guys...
  • wolvorinePk
    wolvorinePk over 11 years
    in tutorial and document update it has not much enough information about it. I need to handle default errors as well as my own customize error for Custom 404 or 405 pages. For that i added error404.php in my system dir and to handle default errors i am using inside config file to the new controller systemcontroller. But somewhat its not working the way i want
  • user2636556
    user2636556 about 10 years
    i had to change it to this if( $error = Yii::app()->errorHandler->error['code'] ) error->code was giving me errors. Also getViewFile is giving me this error. CWebApplication and its behaviors do not have a method or closure named "getViewFile". (/Applications/XAMPP/xamppfiles/htdocs/wm/common/lib/yii/fra‌​mework/base/CCompone‌​nt.php:266) Any ideas?
  • user2636556
    user2636556 about 10 years
    the error message for error->code is Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/wm/backend/controllers‌​/SiteController.php on line 48 @Boris
  • Boris Belenski
    Boris Belenski about 10 years
    You are right. There is no getViewFile method in app class. Should call controler's method: $this->getViewFile(...)