default_controller路由不适用于编码点火器中not_found的功能


default_controller route does not work with a function not_found in codeigniter

我已经使用codeigniter很长时间了。我已将默认控制器设置为:

$route['default_controller'] = "main";

一切都很好,除了我在控制器文件中创建了一个重定向到 404 未找到页面main.php函数。

这是我在main.php中的代码:

function not_found()
{
    $data['page_Title'] = '404 Error!';
    $data['page_Description'] = 'Description';
    $data['page_Keywords'] = 'Keywords';
$data['main_content'] = 'not_found';
    $this->load->view('includes/template', $data); 
}

然后,当我想加载example.com/not_found/时,它会将我重定向到代码点火器的默认未找到页面并说:

404 Page Not Found
The page you requested was not found.

但是,请求这样的页面没有问题:

example.com/main/not_found/

我是否必须设置其他路线,例如:

$route['not_found'] = "main/not_found";

?! 但我认为这不是一个好方法!

CI 的 url 格式描述:

domain.com/controller/method

而不是:

domain.com/method

如果您的控制器中有index function,则该函数将像这样调用:

domain.com

example.com/not_found/ 的意思是,codeIgniter 将找到控制器/not_found.php并执行函数 index()

您必须创建控制器/not_found.php