CodeIgniter access base_url()内部错误_404.php

CodeIgniter access base_url()内部错误_404.php,codeigniter,error-handling,Codeigniter,Error Handling,404页面未找到时发生错误application/errors/error_404.php模板用于CodeIgniter。我需要访问该模板中的base\u url()函数。任何关于如何在error\u 404.php中访问base\u url()或site\u url()函数的建议都非常有用。您可以直接使用这些函数,因为它们是全局函数。 例如:您在404页面模板中 <html> <head> <title> 404 Error </title>

404页面未找到时发生错误
application/errors/error_404.php
模板用于CodeIgniter。我需要访问该模板中的
base\u url()
函数。任何关于如何在
error\u 404.php
中访问
base\u url()
site\u url()
函数的建议都非常有用。

您可以直接使用这些函数,因为它们是全局函数。 例如:您在
404页面模板中

<html> 
<head>
  <title> 404 Error </title>
</head>
<body>
  <?php echo base_url('/your path'); ?>
  <?php echo site_url('/your path'); ?>
</body>
</html>
在此之后,您可以在任何地方直接使用
要使用base_url(),必须首先加载url帮助程序。这可以在application/config/autoload.php中完成
In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php

$autoload['helper'] = array('url');
Or, manually:

$this->load->helper('url');
Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:

echo base_url();

Check if you have something configured inside the config file /application/config/config.php e.g.
$config['base_url'] = 'http://example.com/';

**Then** 
u can use like this..
<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
$autoload['helper']=数组('url'); 或者,手动: $this->load->helper('url'); 加载后,请务必记住base_url()不会隐式打印或回显任何内容,而是返回要打印的值: echo base_url(); 检查您是否在配置文件/application/config/config.php中配置了一些内容,例如。 $config['base_url']='http://example.com/'; **然后** 你可以这样使用。。
application/errors/error_404.php中尝试以下内容

$CI =& get_instance();
if( ! isset($CI))
{
    $CI = new CI_Controller();
}
$CI->load->helper('url');
echo $CI->base_url();

在404页面顶部尝试以下操作:

$CI =& get_instance();
if( ! isset($CI))
{
    $CI = new CI_Controller();
}
$CI->load->helper('url');
echo base_url();

// below here base_url(), site_url() will work

处理视图/错误/html文件夹下的所有页面

编辑您的/application/config/routes.php

$route['404_override'] = 'your_controller/error';
将函数添加到_controller.php中

function error(){
    show_404();   
}

您可以开始了。

$autoload['helper']=array('url');在application\config\autoload.php文件中

然后只需使用echo config_item('base_url');而不是echo base_url();在codeigniter默认错误页面中

参考 或执行以下操作
$autoload['helper']=数组('url');在application\config\autoload.php文件中


然后只需使用echo config_item('base_url');而不是echo base_url();在codeigniter默认错误页面中,您可能需要使用这个简单的技巧。只需更改以下代码:

<?php echo base_url() ?>

致:


功劳
这对我的案例有效

这对我的案例有效,试试这个方法

将函数base\u url()更改为config\u项(“base\u url”)



使用这些函数会直接导致“致命错误:调用未定义的函数base_url()”,您正在使用哪个Codeigniter版本?已经尝试过了。它导致-致命错误:在第234行的D:\xampp\htdocs\developer tools\system\core\CodeIgniter.php中找不到类“CI_Controller”。很高兴我找到了这个。如此恼人以至于CI实例在默认错误模板中丢失+1@Utkanos我认为它是这样设计的-如果错误发生在Codeigniter有机会启动之前怎么办?然后,即使您的get_instance()调用也会返回一个错误。@digout-对于在启动CI之前在PHP级别发生的错误,当然可以。但是这些都是CI提供的错误页,因此期望访问该实例是非常合理的。@Utkanos没有CI可以启动,但无法加载()任何内容。如果你有自动加载的url帮助程序,你就不需要$CI->load->helper('url');
<?php echo base_url() ?>
<?php echo config_item(‘base_url’); ?>
    <?php echo config_item('base_url'); ?>