• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fatal error -> base_url() - what am I doing wrong?

#1
[eluser]ICEcoffee[/eluser]
I get this error when loading a simple view file from my default controller:

Quote:Fatal error: Call to undefined function base_url() in H:\xampp\htdocs\ci2\application\views\home_view.php on line 6

Line from 'home_view.php'
Code:
<link rel='stylesheet' href='<?php echo site_url(’styles/style.css’) ?>' type='text/css' media='screen' charset='utf-8' />

my config file:
Code:
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|    http://www.your-site.com/
|
*/
$config['base_url']    = "http://localhost/ci2/";

#2
[eluser]ontguy[/eluser]
I think you just need to load the url helper before loading the view:
Code:
$this->load->helper('url');

#3
[eluser]ICEcoffee[/eluser]
I followed the suggestion to load the helper, I put it in the index function. I changed the php in the view to include double quotes as per codeigniter user manual:
Code:
<link rel='stylesheet' href='<?php echo base_url("styles/style.css"); ?>' type='text/css' media='screen' charset='utf-8' />

and now I just get a plain page, no applied style, no errors, just text. The browser output is:
Code:
<link rel='stylesheet' href='http://localhost/ci2/' type='text/css' media='screen' charset='utf-8' />

Notice it does not include the path sent to the method/function, according to the browser output.

What else am I doing wrong?

#4
[eluser]ICEcoffee[/eluser]
I changed the code, instead of passing the rest of the path to base_url, I appended it:

Code:
<link rel='stylesheet' href='<?php echo base_url() . "styles/style.css"; ?>' type='text/css' media='screen' charset='utf-8' />


this resulted in the full path to the style folder/file, in the browser output:

Quote:<link rel='stylesheet' href='http://localhost/ci2/styles/style.css' type='text/css' media='screen' charset='utf-8' />
[/code]

but I'm still getting text only ouput and no style formatting. I checked the path was correct, I even felt paranoid enough to check the stylesheet itself just to make sure it still had code in it, and it does.

I think I'm at a loss. Can anyone shed any further light?

#5
[eluser]Lone[/eluser]
So just to verify - if you put the following in your browser as a URL the css file shows?

Code:
http://localhost/ci2/styles/style.css

#6
[eluser]ICEcoffee[/eluser]
I tried that and got a 404. knowing that the path is correct, I checked and removed my .htaccess file, the style page now displays.

I only included it to take out 'index' in the URL. My htaccess file was based on the example given in the codeigniter user guide. can you tell me what is wrong with my htaccess file:

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci2/index.php/$1 [L]

or does it not matter if I intend to rewrite the urls anyway using the url helper?

Sorry if these are basic questions.

#7
[eluser]ontguy[/eluser]
I've been using this one from the CI wiki:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ directory/index.php/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ directory/index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 directory/index.php
</IfModule>

#8
[eluser]ICEcoffee[/eluser]
@ontguy - Thanks for your help, I'll give that a bash. At least we overcame the initial issue of stylesheet not being parsed.

#9
[eluser]Pascal Kriete[/eluser]
Really simple solution. Add styles to your RewriteCond:
Code:
RewriteCond $1 !^(index\.php|styles|images|robots\.txt)

#10
[eluser]evanwalsh[/eluser]
Just so you know, base_url() doesn't take any arguments. However, site_url() does.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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