Welcome Guest, Not a member yet? Register   Sign In
using base_url in errors/error_404.php
#1

[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:
Code:
$CI = '';
$CI =& get_instance();
$CI->load->helper('url');
$CI->base_url();

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.
#2

[eluser]vrencianz[/eluser]
Try out this one:

Code:
$CI =& get_instance();
$CI->load->helper('url');
echo base_url();
#3

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

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

[eluser]vrencianz[/eluser]
Well, I show you what I did to test your issue (fresh codeigniter installation)

1) modified error_404.php as follows:

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

&lt;?php
$CI =& get_instance();
$CI->load->helper('url');
echo base_url();  
?&gt;


2) created a test controller (test.php)

Code:
class Test extends CI_Controller
{
function index()
{
  show_404('test');
}
}

3) accessed
Code:
http://localhost/codeigniter/test
(my base url is set to
Code:
http://localhost/codeigniter/)

and the path is displayed.

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

[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
#6

[eluser]marcogmonteiro[/eluser]
Normally on 404 pages I just use.

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

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

[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.
#8

[eluser]vrencianz[/eluser]
Yep. I see what you mean: if
Code:
http://localhost/codeigniter/test1
(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:

http://maestric.com/doc/php/codeigniter_404
#9

[eluser]CroNiX[/eluser]
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.
#10

[eluser]Aken[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB