Welcome Guest, Not a member yet? Register   Sign In
Using base_url in a view
#1

[eluser]montanaflynn[/eluser]
Maybe I am missing something but I am having trouble echoing the base_url as defined in the config.php in a view. How would you go about doing this? I have tried adding
Code:
<?php echo $base_url;?>
in the view but it gives an error "Message: Undefined variable: base_url"


Some background info:

This is my first time using a PHP framework and I have decided to convert my static site to Codeignitor. This is what I have done so far:

Filled out the system/config/config.php file & routes.php to make home.php the default controller.

Created a home.php controller with the following:
Code:
<?php

    class Home extends Controller {
    function index()
    {
        $data['title'] = "This is the page title";
            $data['desc'] = "My site description is complete!";
            $this->load->view('homeView', $data);
    }
    }
?>


Created a homeView.php view with the following (simplyfied):

Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;

  &lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;
  &lt;meta name="description" content="&lt;?php echo $desc;?&gt;" /&gt;

  &lt;style src="&lt;?php echo $base_url;?&gt;/css/style.css" type="text/css"&gt;
  [removed]
&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?php echo $title;?&gt;</h1>
&lt;/body&gt;
&lt;/html&gt;

Everything works except the base_url, I have a feeling I need to define it in the controller. Any suggestions?
#2

[eluser]John_Betong[/eluser]
&nbsp;
&nbsp;
[email=http://ellislab.com/codeigniter/user-guide/]http://ellislab.com/codeigniter/user-guide/[/email]
&nbsp;
Have a serach in the CodeIgniter User Guide and all will be revealed Smile
&nbsp;
&nbsp;
&nbsp;
#3

[eluser]jdfwarrior[/eluser]
[quote author="John_Betong" date="1251875249"]&nbsp;
&nbsp;
[email=http://ellislab.com/codeigniter/user-guide/]http://ellislab.com/codeigniter/user-guide/[/email]
&nbsp;
Have a serach in the CodeIgniter User Guide and all will be revealed Smile
&nbsp;
&nbsp;
&nbsp;[/quote]

Was it really that hard to just tell him that base_url isn't a variable? That it's a function?

The correct code should be:
Code:
&lt;? echo base_url(); ?&gt;
#4

[eluser]montanaflynn[/eluser]
I found the answer! Thanks for pointing me in the right direction, I had read some of the user guide but hadn't gotten to the helpers section which had the answer.

The solution was as simple as loading the URL helper from the controller like so:

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

And changing
Code:
&lt;?php echo $baseurl;?&gt;
to
Code:
&lt;?php echo base_url();?&gt;
in the view.

Thanks again for the help. I think I will have this helper autoload since I am going to use it in all my views!
#5

[eluser]montanaflynn[/eluser]
[quote author="jdfwarrior" date="1251877201"][quote author="John_Betong" date="1251875249"]&nbsp;
&nbsp;
[email=http://ellislab.com/codeigniter/user-guide/]http://ellislab.com/codeigniter/user-guide/[/email]
&nbsp;
Have a serach in the CodeIgniter User Guide and all will be revealed Smile
&nbsp;
&nbsp;
&nbsp;[/quote]

Was it really that hard to just tell him that base_url isn't a variable? That it's a function?

The correct code should be:
Code:
&lt;? echo base_url(); ?&gt;
[/quote]

Its ok, I enjoy the challenge. Just to make sure, you have to load the URL helper as well, right? I tried to take it out of the controller and the page became blank.
#6

[eluser]John_Betong[/eluser]
>>> Just to make sure, you have to load the URL helper as well, right?
>>> I tried to take it out of the controller and the page became blank.
&nbsp;
I reckon that now is a good time to check out the User Guide for the "Log file".
See where the Log file is and what error is shown.
&nbsp;
PS A long time ago I was taught something like it was not what you actually know it is knowing where to look and to implement for the solution".
&nbsp;
&nbsp;
&nbsp;
#7

[eluser]montanaflynn[/eluser]
Ahh I had to enable it in the config.php file. Thanks!

Again thanks for the help and sorry I am such a beginner, but I guess everyone starts somewhere.
#8

[eluser]überfuzz[/eluser]
[quote author="montanaflynn" date="1251879060"]Again thanks for the help and sorry I am such a beginner, but I guess everyone starts somewhere.[/quote]
Most start with the user guide. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB