我尝试处理Yii2中的HttpExceptions以显示Webusers的错误消息。我把一切都弄好喜欢这里:http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html在视图中显示Yii2 HttpException消息
控制器
namespace app\controllers;
use Yii;
use yii\web\Controller;
class SiteController extends Controller
{
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
}
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $this->render('error', ['exception' => $exception]);
}
}
当我把这样的错误:
throw new HttpException(404,"This is an error. Maybe Page not found!");
我想显示的文字在我看来,文件或至少文档中描述的变量 - 但变量变量被保护或私有。任何想法如何做到这一点?
查看
$exception->statusCode // works
$exception->message // proteced
谢谢,我知道了。像你描述的那样工作。但是如何显示我在HttpException中设置的消息文本? – kasoft