繁体   English   中英

如何在 CodeIgniter 4 中找不到自定义 404 页面

[英]How to Custom 404 Page Not Found in CodeIgniter 4

我刚刚学习了 CodeIgniter 4 框架。 如何自定义我的 404 页面未找到?

Go 到您的 Routes.php 文件并找到

$routes->set404Override();

现在,如果您只想显示一条错误消息,请编写

    $routes->set404Override(function(){
    echo "your error message";
});

代替

$routes->set404Override();

或者如果你想返回一个视图,那么写如下:

$routes->set404Override(function(){
    return view('your_filename');
});

代替

$routes->set404Override();

// 将执行 App\Errors class 的 show404 方法

$routes->set404Override('App\Errors::show404');

// 将显示一个自定义视图

$routes->set404Override(function()
{
    echo view('my_errors/not_found.html');
});

要自定义 404 页面,请根据自己的喜好修改app/Views/errors/html/error_404.php

对于自定义错误消息

在 Config\routes.php

// Custom 404 view with error message.
$routes->set404Override(function( $message = null )
{
    $data = [
        'title' => '404 - Page not found',
        'message' => $message,
    ];
    echo view('my404/viewfile', $data);
});

在我的视图文件中,显示错误:

<?php if (! empty($message) && $message !== '(null)') : ?>
  <?= esc($message) ?>
<?php else : ?>
  Sorry! Cannot seem to find the page you were looking for.
<?php endif ?>

来自我的 controller:

throw new \CodeIgniter\Exceptions\PageNotFoundException('This is my custom error message');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM