Php 在视图中显示Yii2 HttpException消息
我尝试在Yii2中处理HttpExceptions,以便为Web用户显示错误消息。我把一切都安排在这里: 控制器Php 在视图中显示Yii2 HttpException消息,php,yii2,httpexception,Php,Yii2,Httpexception,我尝试在Yii2中处理HttpExceptions,以便为Web用户显示错误消息。我把一切都安排在这里: 控制器 namespace app\controllers; use Yii; use yii\web\Controller; class SiteController extends Controller { public function actions() { return [ 'error' => [
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!");
我想在我的视图文件中显示文本,或者至少显示文档中描述的变量,但是所有变量都是受保护的或私有的。有什么办法吗
看法
首先,您将两次定义
错误
操作,一次定义为siteController的方法,第二次定义为操作
方法。
可以使用视图文件中的“$message”变量检索错误消息,使用$exception->message
是不正确的。
Yii文档允许在错误视图文件中使用这些变量
- 名字
- 信息
- 例外情况
错误
操作定义两次,一次作为siteController的方法,第二次是在操作
方法中。
可以使用视图文件中的“$message”变量检索错误消息,使用$exception->message
是不正确的。
Yii文档允许在错误视图文件中使用这些变量
- 名字
- 信息
- 例外情况
views\site\error.php中检查了视图文件。我花了很长时间才意识到这是用来显示错误页面的
<?php
/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */
use yii\helpers\Html;
$this->title = $name;
?>
<div class="site-error">
<h1><?= Html::encode($this->title) ?></h1>
<div class="alert alert-danger">
<?php /* this is message you set in `HttpException` */ ?>
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
<?= Yii::t('app', 'Here is text that is displayed on all error pages') ?>
</p>
</div>
不确定是否在views\site\error.php
中检查了视图文件。我花了一段时间才意识到这是用来显示错误页面的
<?php
/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */
use yii\helpers\Html;
$this->title = $name;
?>
<div class="site-error">
<h1><?= Html::encode($this->title) ?></h1>
<div class="alert alert-danger">
<?php /* this is message you set in `HttpException` */ ?>
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
<?= Yii::t('app', 'Here is text that is displayed on all error pages') ?>
</p>
</div>
试试这个
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
$model->save()
$transaction->commit();
return $this->redirect(['user/view', 'id' => $model->id]);
}catch (\Exception $e) {
$transaction->rollBack();
throw new \yii\web\HttpException(500,"YOUR MESSAGE", 405);
}
试试这个
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
$model->save()
$transaction->commit();
return $this->redirect(['user/view', 'id' => $model->id]);
}catch (\Exception $e) {
$transaction->rollBack();
throw new \yii\web\HttpException(500,"YOUR MESSAGE", 405);
}
也许您可以尝试添加额外的内容,并像这样扩展Yii2错误消息中的异常。
只需创建一个名为ErrorMsg.php的新自定义php文件
<?php
use Yii;
use yii\web\HttpException;
class ErrorMsg extends \Exception
{
public static function customErrorMsg($error_code,$message = null, $code = 0, \Exception $previous = null,$extra_content=NULL)
{
$httpException = new HttpException($error_code,$message,$code,$previous);
Yii::$app->response->statusCode = $error_code;
$custom_err = array(
'name'=> $httpException->getName(),
'message' => $message,
'code' => $code,
'extraContent' => $content,
'status' => $error_code,
'type' => "\\utilities\\ErrorMsg"
);
return $custom_err;
}
也许您可以尝试添加额外的内容,并像这样扩展Yii2错误消息中的异常。
只需创建一个名为ErrorMsg.php的新自定义php文件
<?php
use Yii;
use yii\web\HttpException;
class ErrorMsg extends \Exception
{
public static function customErrorMsg($error_code,$message = null, $code = 0, \Exception $previous = null,$extra_content=NULL)
{
$httpException = new HttpException($error_code,$message,$code,$previous);
Yii::$app->response->statusCode = $error_code;
$custom_err = array(
'name'=> $httpException->getName(),
'message' => $message,
'code' => $code,
'extraContent' => $content,
'status' => $error_code,
'type' => "\\utilities\\ErrorMsg"
);
return $custom_err;
}
谢谢,我知道了。就像你描述的那样。但是如何显示我在HttpException中设置的消息文本呢?谢谢,我知道了。就像你描述的那样。但是如何显示我在HttpException中设置的消息文本呢?