I have a controller "front" and I want only for this controller to use a custom error action.
How can I do this?
I have a controller "front" and I want only for this controller to use a custom error action.
How can I do this?
Hi,
Like described here:
http://www.yiiframework.com/doc-2.0/guide-runtime-handling-errors.html#using-error-actions
Additional:
http://www.yiiframework.com/doc-2.0/yii-web-erroraction.html
Regards
I tried this but as I understand it works for the default error action,for me the site/error,bellow the web.php
errorHandler' => [
'errorAction' => 'site/error',
],
I tried at front controller the code bellow but it does not work.
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
'view' => '@app/views/front/error.php'
],
];
}
[edit]I managed to solve it!
I managed to write the code bellow but now the error view does not have the layout,how can I solve this?
if ($model == null) {
$exception = new \yii\web\NotFoundHttpException("The patient does not exist");
$statusCode = $exception->statusCode;
$name = $exception->getName();
$message = $exception->getMessage();
return $this->render('error', [
'exception' => $exception,
'statusCode' => $statusCode,
'name' => $name,
'message' => $message
]);
}
the 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">
<?= nl2br(Html::encode($message)) ?>
</div>
<p>
The above error occurred while the Web server was processing your request.
</p>
<p>
Please contact us if you think this is a server error. Thank you.
</p>
</div>
Here is a nice View
<?php /* @var $this yii\web\View */ /* @var $message string */ /* @var $exception Exception */ use yii\helpers\Html; $this->title = $exception->statusCode; ?><h1 class="text-center"><?= Html::encode($this->title) ?></h1>
<div class="alert alert-danger text-center">
<?= nl2br(Html::encode($exception->getMessage())) ?>
</div>
<?php
if (YII_DEBUG) {
?>
<p class="alert alert-danger">
<?= nl2br(Html::encode($exception)) ?>
</p>
<?php
} else {
?>
<p>
The above error occurred while the Web server was processing your request.
</p>
<p>
Please contact us <?= (isset(Yii::$app->params['adminEmail']) ? 'at <a href="mailto:' . Yii::$app->params['adminEmail'] . '">' . Yii::$app->params['adminEmail'] . '</a>' : '') ?> if you think this is a server error. Thank you.
</p>
<?php
}
?>