The base_url () function will not work on error pages. Even after startup

I want to have good pages with good search. For this I need to get some CSS and JS files. But for some odd reason, using base_url() doesn't work on error_pages. I could just use href="/css/style.css" and say to get it from the root folder. But the site may well be placed in a different folder than the root folder. Therefore, using / not an option.

So now my question is why base_url() not working on the error page? I automatically downloaded it so that it does not work?

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

In my autoload.php, I included a helper url

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

And on my error_404 page (application / views / errors / html / error_404.php) I repeat base_url like this:

<?php echo base_url(); ?>

This is the error I get:

 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 do not want to make changes to the system folder, since I do not want to repeat this process every time I update the Codeigniter folder.

System:

Codeigniter 3.0.6

PHP 7.0.8

Nginx 1.11.1

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

+6
source share
3 answers

The autoloader does not work because the autoloader loads the libraries after the error checking is complete. If there are errors, such as 404 not found , it will look at the user errors page without loading the autoloader.

To get base_url on a custom page, you need to add the following on a custom error page:

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

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

+5
source

You must load the helper class in order to access the functions of the URL. So first try doing:

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

Then you can repeat it using

 echo base_url(); 

you can get the same result with

 $this->config->config['base_url']; 
+1
source

To do this, you should redefine the error pages in the routes.php file like this:

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

And inside the method() function, you can access base_url() after loading the uri helper just like a regular codeigniter controller.

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

if we need to redesign the other php / db error pages. We can edit the following files enter image description here

0
source

All Articles