Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to pass CHttpException an HTML message string?

Tags:

php

yii

I'm handling 404s with the line CHttpException(404, $message), and the $message variable is an HTML string to achieve some styling. But CHttpException() encodes my $message, rendering the HTML as a useless escaped string. Is there any way around the string encoding of CHttpException, in order to output HTML?

/**
* From PostsController.php
*/

// ...

public function actionCategory($id)
{

   $categoryModel = Categories::model()->findByAttributes(array('idCategory'=>$id));

    if($categoryModel===null){

        $message = "<div class=\"alert alert-danger\">Page Not Found</div>";
        throw new CHttpException(404, $message);
            return;
        }

    // ...
like image 992
eric Avatar asked Sep 15 '25 13:09

eric


1 Answers

CHttpException does not encode anything. By default message is encoded in error view which is in protected/views/site/error.php - I suppose you only have to change it in that view (or your custom error view if you did change that). Only you should be aware that standard errors thrown by yii are not prepared for HTML so you should find some way to distinguish between CHttpException thrown by your code and that thrown by Yii.

like image 106
lupatus Avatar answered Sep 17 '25 02:09

lupatus