我尝试在 Yii2 中处理 HttpExceptions 以向 Web 用户显示错误消息。我在这里设置一切:http ://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html
控制器
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