custom 404 page not found in yii?

how do i setup custom 404 page not found pages with yii?

You can create custom error views under protected/views/system. In that dir, create error404.php.

is there any way to use the protected/views/layouts/main.php layout in the error404.php page?

while retaining the 404 "page not found" header?

You can create an action in a controller that handles the 404, and is thus created like a regular view with a layout.

Check out this link to the manual,

Error Handling

There’s a section called ‘Handling Errors with an Action’.

here is a quick walk-through:

config/main.php




'components'=>array(

...

        'errorHandler'=>array(

            'errorAction' => 'site/error'

        ),

...

),



controllers/SiteController.php




    public function actionError()

    {

        $error = Yii::app()->errorHandler->error;


        if( $error )

        {

            $this -> render( 'error', array( 'error' => $error ) );

        }

    }



views/site/error.php




<div class="errorSummary">

    <p>

    Sorry, it seems like a(n) <?= $error['code']; ?> error has occured during your request.

    </p>


    <p><strong>Message:</strong> <?= $error['message']; ?></p>

</div>



I hope this helps.

–iM

thank you imehesz!

much easier to understand than the manual (which took me over 6 reads to even get a jist of it)

just 1 question:

this comes with a 404 header, right?

how would i go about modifying the header error code? (to for example a 403 code)

hi,

Well, the error code is in the $error variable, so I guess you could change the header information in the controller before rendering the view, but I haven’t done that :confused:

–iM

thx imehesz, currently the 404 will be fine.

also with this action error method do i need to create a custom error page for every Controller?

for example will each one of my controllers require the

controllers/SiteController.php


    public function actionerror()

    {

        $error = Yii::app()->errorHandler->error;


        if( $error )

        {

            $this -> render( 'error', array( 'error' => $error ) );

        }

    }

controllers/HelpController.php


    public function actionerror()

    {

        $error = Yii::app()->errorHandler->error;


        if( $error )

        {

            $this -> render( 'error', array( 'error' => $error ) );

        }

    }

controllers/StuffController.php


    public function actionerror()

    {

        $error = Yii::app()->errorHandler->error;


        if( $error )

        {

            $this -> render( 'error', array( 'error' => $error ) );

        }

    }

sorry if the question is newbish, im completely new to frameworks altogether.

hi,

no, that’s the only one you’ll need!

that’s why we have forums ;) Yii is definitely one of the best frameworks out there.

–iM

ps.: welcome to Yii :D

ok, i got it working today with your walkthrough and some new understanding of the framework, basically i made a new SystemController (same as SiteController but just all the words in it renamed and unnecessary stuff removed) edited the config/main.php to lead errors to system/error

and put the error.php file in the system folder!

works! yay!!

thanks a lot guys (and thanks for the friendly welcome)