CodeIgniter Forums
Undefined variable: base_url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Undefined variable: base_url (/thread-67606.html)



Undefined variable: base_url - edmilner - 03-15-2017

Hi, I am getting the following error when trying to use $base_url in a view:


Message: Undefined variable: base_url
Filename: templates/header.php


Message: Function name must be a string
Filename: templates/header.php

From researching this, the most common reason for this is that the 'url' helper has not been loaded but I have added it to autoload.php

/application/config/autoload.php:


PHP Code:
$autoload['helper'] = array('url'); 

I have also checked the $base_url variable in config.php:


PHP Code:
$config['base_url'] = 'http://somedomain.com/folder/'; <--domain name changed from original 


And this is the php of the view:


PHP Code:
<link href="<?php echo $base_url(); ?>css/bootstrap.min.css" rel="stylesheet"


This is how the view is been loaded from the controller:


PHP Code:
    function index()
    {
        $data['tasks'] = $this->Task_model->get_all_tasks();
        $this->load->view('templates/header');
        $this->load->view('task/index',$data);
        $this->load->view('templates/footer');
    


If I changed it to use a local variable it works fine:


PHP Code:
<?php $test "http://somedomain.com/folder/" ?>
<link href="<?php echo $test?>css/bootstrap.min.css" rel="stylesheet"> 

I am new to this so it could be something simple but it is although the config file and/or autoload file is not linked in properly, corrupt or been overidden somehow. Any suggestions welcome.


RE: Undefined variable: base_url - Narf - 03-15-2017

It is simple - it's a function, not a variable (and you're trying to use it as a variable function).


RE: Undefined variable: base_url - edmilner - 03-15-2017

Thanks, yes I spotted my mistake. should be <?php echo base_url() ?> not <?php echo $base_url() ?>.


RE: Undefined variable: base_url - kierownik - 03-15-2017

PHP Code:
<link href="<?php echo $base_url(); ?>css/bootstrap.min.css" rel="stylesheet"

should be

PHP Code:
<link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet"