1

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

4

4 回答 4

3

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

$routes->set404Override();

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

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

代替

$routes->set404Override();

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

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

代替

$routes->set404Override();
于 2020-05-06T08:58:21.333 回答
0

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

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

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

$routes->set404Override(function()
{
    echo view('my_errors/not_found.html');
});
于 2020-04-30T14:13:24.650 回答
0

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

于 2020-04-30T14:36:54.657 回答
0

对于自定义错误消息

在 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 ?>

从我的控制器:

throw new \CodeIgniter\Exceptions\PageNotFoundException('This is my custom error message');
于 2020-10-17T17:07:45.123 回答