Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 708
wolvorinePk Avatar asked Aug 05 '12 10:08

wolvorinePk


People also ask

How do I redirect 404 error to custom error page?

Apache provides ErrorDocument directive to help you catch all types of error responses. So if you want to redirect all 404 errors to a specific web page (e.g http://www.example.com/error.html) just add the following line to your . htaccess file. The above line will rewrite 404 error to URL you have mentioned.

How do you handle a 404 page?

Use Redirects When Possible Often, it's best to not show a 404 page at all, and instead simply redirect people to a different page. If you update a page and change the URL, make sure you redirect the old URL to the new page so visitors don't wind up on a 404 page.

How do I customize my WordPress 404 page?

In the left-hand menu of the WordPress Admin Dashboard, go to Appearance -> 404 Error Page. Select the page you have just customized as your 404 page and set it as 404-error page that'll be displayedappear by default, when users land on a broken link: Click Save Changes and that's it.


2 Answers

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
like image 32
bingjie2680 Avatar answered Jan 13 '23 21:01

bingjie2680


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.

like image 106
Boris Belenski Avatar answered Jan 13 '23 22:01

Boris Belenski