函数的作用是:在错误页面上不起作用。即使在半自动的

时间:2022-10-06 16:37:33

I want to have nice good looking error pages. For this I need to get some CSS and JS files. But for some odd reason using base_url() does not work on the error_pages. I could of course just use href="/css/style.css" and tell it to get it from the root folder. But the website could very well be put in a different folder than the root folder. So using the / is not an option.

我希望有好看的错误页面。为此,我需要一些CSS和JS文件。但是出于某种奇怪的原因,使用base_url()对error_pages不起作用。我当然可以使用href="/css/style。并告诉它从根文件夹中获取。但是这个网站很可能被放在一个不同于根文件夹的文件夹中。所以使用/不是一个选项。

So my question now is why doesn't base_url() work on an error page? I have autoloaded it so shouldn't it be working?

我现在的问题是为什么base_url()不能在错误页面上工作?我已经自动检查过它了,它不应该工作吗?

This is what I tried when I was trying to get the base_url() from the error_404 page in the view.

这是我在试图从视图的error_404页面获取base_url()时所尝试的。

In my autoload.php I have included the helper url

在我的自动装载。我已经包含了帮助url

$autoload['helper'] = array('url', 'form');

And in my error_404 page (application/views/errors/html/error_404.php) I am echoing the base_url like so:

在我的error_404页面(应用程序/视图/错误/html/error_404.php)中,我重复了base_url,如下所示:

<?php echo base_url(); ?>

< ?php echo base_url();? >

This is the error I'm getting:

这是我得到的错误:

An uncaught Exception was encountered

Type: Error

Message: Call to undefined function base_url()

Filename: /usr/local/www/example.com/application/views/errors/html/error_404.php

Line Number: 37

Backtrace:

    File: /usr/local/www/example.com/index.php
    Line: 315
    Function: require_once

I'd rather not make any changes in the system folder since I don't want to redo the process every time I update Codeigniter folder.

我不想在系统文件夹中做任何更改,因为我不想在每次更新Codeigniter文件夹时都重新执行这个过程。

System:

系统:

Codeigniter 3.0.6

Codeigniter 3.0.6

PHP 7.0.8

PHP 7.0.8

Nginx 1.11.1

Nginx 1.11.1

UPDATE: I'd like to call base_url on every error page (db, 404, general, php, etc.).

更新:我想在每个错误页面上调用base_url (db, 404, general, php等)。

3 个解决方案

#1


5  

The autoloader does not work because the autoloader loads the libraries after the error checks are finished. If there are errors like a 404 not found it will view the custom error page without loading the autoloader.

自动加载器不能工作,因为自动加载器在错误检查完成后加载库。如果有像404未找到这样的错误,它将在不加载自动加载程序的情况下查看自定义错误页面。

To get your base_url on a custom page you would need to add the following in your custom error page:

要在自定义页面上获取base_url,您需要在自定义错误页面中添加以下内容:

$base_url = load_class('Config')->config['base_url'];

Taken from: http://forum.codeigniter.com/thread-65968-post-335822.html#pid335822

来自:http://forum.codeigniter.com/thread - 65968 - post - 335822. - html # pid335822

#2


1  

You must load the helper class to get access url functions. So, try to do first:

必须加载helper类以获得访问url函数。所以,试着先做:

$this->load->helper('url');

Then you can echo it using

然后您可以使用它进行回显

echo base_url();

alternatively, you can get the same result with

或者,您也可以得到相同的结果

$this->config->config['base_url'];

#3


0  

In order to do this ,You must override the error pages in your routes.php file something like

为了做到这一点,您必须重写路由中的错误页。php文件之类的

$route['404_override'] = 'controller/method';

And inside method() function,You can access base_url() after loading uri helper as same as normal codeigniter controller.

在method()函数中,可以像普通的codeigniter控制器一样在加载uri helper之后访问base_url()。

$this->load->helper('uri');
echo base_url();

if we need to change the design of other php/db error pages.we can edit the following files 函数的作用是:在错误页面上不起作用。即使在半自动的

如果我们需要更改其他php/db错误页面的设计。我们可以编辑以下文件。

#1


5  

The autoloader does not work because the autoloader loads the libraries after the error checks are finished. If there are errors like a 404 not found it will view the custom error page without loading the autoloader.

自动加载器不能工作,因为自动加载器在错误检查完成后加载库。如果有像404未找到这样的错误,它将在不加载自动加载程序的情况下查看自定义错误页面。

To get your base_url on a custom page you would need to add the following in your custom error page:

要在自定义页面上获取base_url,您需要在自定义错误页面中添加以下内容:

$base_url = load_class('Config')->config['base_url'];

Taken from: http://forum.codeigniter.com/thread-65968-post-335822.html#pid335822

来自:http://forum.codeigniter.com/thread - 65968 - post - 335822. - html # pid335822

#2


1  

You must load the helper class to get access url functions. So, try to do first:

必须加载helper类以获得访问url函数。所以,试着先做:

$this->load->helper('url');

Then you can echo it using

然后您可以使用它进行回显

echo base_url();

alternatively, you can get the same result with

或者,您也可以得到相同的结果

$this->config->config['base_url'];

#3


0  

In order to do this ,You must override the error pages in your routes.php file something like

为了做到这一点,您必须重写路由中的错误页。php文件之类的

$route['404_override'] = 'controller/method';

And inside method() function,You can access base_url() after loading uri helper as same as normal codeigniter controller.

在method()函数中,可以像普通的codeigniter控制器一样在加载uri helper之后访问base_url()。

$this->load->helper('uri');
echo base_url();

if we need to change the design of other php/db error pages.we can edit the following files 函数的作用是:在错误页面上不起作用。即使在半自动的

如果我们需要更改其他php/db错误页面的设计。我们可以编辑以下文件。