• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using base_url in errors/error_404.php

[eluser]Abishek R Srikaanth[/eluser]
I would like to use the base_url helper function on the errors/error_404.php and I am having trouble using it. I have tried the following ways.

Method 1:
1. Auto Loaded the URL helper.
2. Used base_url

Got the following error message when doing Method 1
"Call to an undefined function base_url"

Method 2:
$CI = '';
$CI =& get_instance();

Get the following error when using Method 2.
"Class 'CI_Controller' not found in /var/.......//system/core/CodeIgniter.php on line 233"

Could someone please help me on this.

Try out this one:

$CI =& get_instance();
echo base_url();

[eluser]Abishek R Srikaanth[/eluser]
Get the following error

“Class ‘CI_Controller’ not found in /var/.......//system/core/CodeIgniter.php on line 233”

Well, I show you what I did to test your issue (fresh codeigniter installation)

1) modified error_404.php as follows:

<div id="container">
  <h1>&lt;?php echo $heading; ?&gt;</h1>
  &lt;?php echo $message; ?&gt;

$CI =& get_instance();
echo base_url();  

2) created a test controller (test.php)

class Test extends CI_Controller
function index()

3) accessed
(my base url is set to

and the path is displayed.

Maybe you must check your base url in config.php.

[eluser]Abishek R Srikaanth[/eluser]
The error_404.php is also called automatically when a controller is not found. So in this case it will fail.

For example if Controller Test never existed and if someone visited http://domainname.com/Test. This would bring the error_404.php

Normally on 404 pages I just use.

&lt;?= $_SERVER['SERVER_NAME'] ?&gt;

And that would have the following result: www.example.com

[eluser]Abishek R Srikaanth[/eluser]
Thanks ur suggestion worked great.

From what I have understood with all the trial and error methods and also from the answer of vrencianz, the error pages have an instance of CI_Controller only if show_404 is called from within a Controller. With this instance one could achieve as described on the answer of vrencianz.

But in cases where the error pages are called implicitly without an instance of the controller (For example: when a controller is not found), the CI_Controller instance is not found in this case and therefore base_url would not work.

So the wisest solution would be to use the $_SERVER['SERVER_NAME'] instead of base_url in the error pages.

Thanks marcogmonteiro and vrencianz for the help in helping me understand how it actually works.

Yep. I see what you mean: if
(non-existent page) is accessed then <i>Fatal error: Class 'CI_Controller' not found in blabla/CodeIgniter.php on line 233</i> appears.

That means that there is no controller instance when the url points to a non-existent one.

If you really want to use helpers, views in your 404 responses take a look to this:


It's because when something is missing, it triggers the 404 before the rest of CI is loaded (doesn't need to load everything if it's a missing resource which it checks for at the bottom of CodeIgniter.php), so some basic functions aren't available to the error_404().

I'm not sure if setting a '404_override' (see routes) controller fixes that, but it probably does.

The downside of the 404_override route is that it does not affect the show_404() function, which calls the error_404.php file. That's one thing that I think CI could really use, is all framework-powered error pages.

Also, for what it's worth, you can always include the helper files the old fashioned PHP way. Some of them might not work since they utilize CI classes inside of the functions, but standard HTML functions and stuff work great still.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.