Sunday, April 21, 2013

Yii throw 404 custom exception

You need to define a view file under protected/views/site folder (any folder and that shoud be configured in config/main.php file as below) suppose named error.php, and throw CHttpException from where you want to show error page.


Define error view in config/main.php file:
'errorHandler'=>array(
    // use 'site/error' action to display errors
    'errorAction'=>'site/error',
)
That means a file named error.php shoud be in views/site/ folder.

Throw 404 custom exception using the following php  code:
throw new CHttpException(404,'The requested page does not exist.');

And the error.php file look like:
<?php
$this->pageTitle=Yii::app()->name . ' - Error';
$this->breadcrumbs=array(
    'Error'
);
?>
<h2>Error <?php echo $code; ?></h2>
<div class="error">
<?php echo CHtml::encode($message); ?>
</div>

No comments:

Post a Comment