Skip to content

Instantly share code, notes, and snippets.

@defrindr
Created March 19, 2021 06:16
Show Gist options
  • Save defrindr/e2f7e64f784063c7d858711876432cc7 to your computer and use it in GitHub Desktop.
Save defrindr/e2f7e64f784063c7d858711876432cc7 to your computer and use it in GitHub Desktop.
How to custom error page in yii2 ??

In controllers\SiteController.php change the code like this.

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
                'layout' => 'error-layout', // the rendered layout
                'view' => 'error-page' // the rendered view
            ],
            ...
        ];
    }
    ```
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\assets;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class ErrorAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [];
public $js = [];
public $depends = [
// 'yii\web\YiiAsset',
'yii\bootstrap\BootstrapPluginAsset',
'yii\bootstrap\BootstrapAsset',
];
}
<?php
use app\assets\ErrorAsset;
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */
ErrorAsset::register($this)
?>
<!DOCTYPE html>
<?php $this->beginPage()?>
<html lang="en">
<head>
<link rel="icon" type="image/png" href=<?=\Yii::$app->request->baseUrl . "/uploads/" . $modelTentang->logo?> />
<meta charset="<?=Yii::$app->charset?>" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<?=Html::csrfMetaTags()?>
<title><?=($this->title) ? Html::encode($this->title) : "Next Trip SS"?></title>
<?php $this->head()?>
<style>
/*======================
404 page
=======================*/
.page_404 {
padding: 40px 0;
background: #fff;
font-family: 'Arvo', serif;
}
.page_404 img {
width: 100%;
}
.four_zero_four_bg {
background-image: url(https://cdn.dribbble.com/users/285475/screenshots/2083086/dribbble_1.gif);
height: 400px;
background-position: center;
}
.four_zero_four_bg h1 {
font-size: 80px;
}
.four_zero_four_bg h3 {
font-size: 80px;
}
.link_404 {
color: #fff !important;
padding: 10px 20px;
background: #39ac31;
margin: 20px 0;
display: inline-block;
}
.contant_box_404 {
margin-top: -50px;
}
</style>
</head>
<body>
<?php $this->beginBody()?>
<?= $content ?>
<?php $this->endBody()?>
</body>
</html>
<?php $this->endPage()?>
<?php
use yii\helpers\Html;
$this->title = $name;
?>
<section class="page_404">
<div class="container">
<div class="row">
<div class="col-sm-12 ">
<div class="col-sm-10 col-sm-offset-1 text-center">
<div class="four_zero_four_bg">
<h1 class="text-center "><?= $name ?></h1>
</div>
<div class="contant_box_404">
<h3 class="h2">
Look like you're lost
</h3>
<p><?= nl2br(Html::encode($message)) ?></p>
<a href="<?= Yii::$app->homeUrl?>" class="link_404">Go to Home</a>
</div>
</div>
</div>
</div>
</div>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment