68

In my web application using codeigniter. I am trying to use base_url() function but it shows empty results. I have also used autoload helper through autoload file, but then too it doesn't seem to work. Also I had defined base constants but all in vain.

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title><?php echo $title; ?></title>        
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
        <script type="text/javascript">
            //<![CDATA[
            base_url = '<?= base_url();?>';
            //]]>
        </script>
    </head>
Guillaume Raymond
  • 1,726
  • 1
  • 20
  • 33
Sanks R
  • 895
  • 1
  • 11
  • 17
  • 1
    Can you show us your code, please? – Sampson Jun 23 '11 at 04:37
  • @Jonathan Sampson: I have tried to use echo base_url(); in the main body section and got an empty result – Sanks R Jun 23 '11 at 04:49
  • 1
    @Sanks Can you show us the generated HTML? I see that you're using both short tags for printing in the script area, and standard echo for output in the stylesheet region. – Sampson Jun 23 '11 at 04:49
  • Can you place `load->helper('url'); ?>` to the top of your view and try once more? – Sampson Jun 23 '11 at 04:51
  • 1
    @Jonathan Sampson: Thanks man , this thing worked $this->load->helper('url'). But I want to know why the helper is not loading automatically? Why the hell we have to print this thing at the top? – Sanks R Jun 23 '11 at 04:55
  • 3
    @Sanks Look to `application/config/autoload.php` at line 67. Make sure you see `$autoload['helper'] = array('url');`. – Sampson Jun 23 '11 at 04:57

12 Answers12

157

In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67):

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

Or, manually:

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

Once it's loaded, be sure to keep in mind that base_url() doesn't implicitly print or echo out anything, rather it returns the value to be printed:

echo base_url();

Remember also that the value returned is the site's base url as provided in the config file. CodeIgniter will accomodate an empty value in the config file as well:

If this (base_url) is not set then CodeIgniter will guess the protocol, domain and path to your installation.

application/config/config.php, line 13

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 1
    Guessing that an `echo` or something was missing? Otherwise this should not happen, autoload will choke if the helper was spelled wrong or doesn't exist. At least php would throw an error if the function was not defined. `base_url()` should always return *something*. **Addendum**: Note that in 2.0.2 the config value can be empty and will auto-detect the base url, so there's no chance of it being empty. – Wesley Murch Jun 23 '11 at 04:41
  • @Wesley I'm suspecting it's a missing call to `echo` as well. – Sampson Jun 23 '11 at 04:43
  • @Jonathan Sampson: I have loaded the autoload helper as said. But still it doesn't seem to work. I have gone through codeigniter user guide and I think URL helper exists. – Sanks R Jun 23 '11 at 04:46
  • @SanksR Are you placing `echo` before the call or not? Please update your question with relevant code so that we can better assist you. – Sampson Jun 23 '11 at 04:47
  • CI treats `=` as `echo`, what I understand is Jonathan has more or less correctly found the issue. Load URL helper and make sure bas_url is set in config. Refer to http://codeigniter.com/user_guide/general/alternative_php.html – Kumar Jun 23 '11 at 04:50
  • @Kumar I believe short-tag rewriting is turned off by default in `config.php`. I'm not too worried about that though, since the OP also used `echo` in the code sample yet reports that this too resulted in no output. – Sampson Jun 23 '11 at 04:56
  • @Jonathan, yes you are right, I have added a user guide link to my comment, and as you say that is not a point to worry about. I suspect that either the file helper is not loaded or the base_url is not set in config. – Kumar Jun 23 '11 at 04:58
  • 1
    @Jonathan Sampson: Thank you man. Its working fine now using autoload helper. The thing is that I was declaring autoload thing for more than once. – Sanks R Jun 23 '11 at 04:59
  • @Kumar The OP has confirmed that manually loading the helper in the view solves the problem. I'm suspecting either there's a problem in the auto-load portion of the process. Perhaps an entire absence of 'url' in the array, or a typo. – Sampson Jun 23 '11 at 05:00
  • @SanksR To auto-load many things, just increase the array: `$autoload['helper'] = array('url', 'html', 'form');`. Don't redeclare `$autoload['helper']` :) – Sampson Jun 23 '11 at 05:01
8

If you want to use base_url(), so we need to load url helper.

  1. By using autoload $autoload['helper'] = array('url');
  2. Or by manually load in controller or in view $this->load->helper('url');

Then you can user base_url() anywhere in controller or view.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
prash.patil
  • 687
  • 8
  • 7
4

First of all load URL helper. you can load in "config/autoload.php" file and add following code $autoload['helper'] = array('url');

or in controller add following code

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

then go to config.php in cofig folder and set

$config['base_url'] = 'http://urlbaseurl.com/';

hope this will help thanks

Ravi Mane
  • 1,468
  • 2
  • 18
  • 24
3

I think you haven't edited codeigniter files to enable base_url(). you try to assign it in url_helper.php you also can do the same config/autoload.php file. you can add this code in your autoload.php

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

Than You will be able to ue base_url() like this

<link rel="stylesheet" href="<?php echo base_url();?>/css/template/default.css" type="text/css" />
Sujeet Kumar
  • 1,280
  • 1
  • 17
  • 25
3

Check if you have something configured inside the config file /application/config/config.php e.g.

$config['base_url'] = 'http://example.com/';
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
reymundolopez
  • 116
  • 1
  • 5
2

Apart from making sure you have set config/autoload.php:

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

Change application/config/config.php from:

$config['base_url'] = 'http://example.com/';

Become a dynamic base url:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https": "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

And using the host from php to run it on local, below is just an example port.

php -S localhost:2000
Dharman
  • 30,962
  • 25
  • 85
  • 135
Hana Hasanah
  • 145
  • 1
  • 6
0

If you don't want to use the url helper, you can get the same results by using the following variable:

$this->config->config['base_url']

It will return the base url for you with no extra steps required.

pbarney
  • 2,529
  • 4
  • 35
  • 49
0

Load url helper in controller

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

Nooha Haris
  • 41
  • 1
  • 4
0

Anything if you use directly in the Codeigniter framework directly, like base_url(), uri_string(), or word_limiter(), All of these are coming from some sort of Helper function of framework.

While some of Helpers may be available globally to use just like log_message() which are extremely useful everywhere, rest of the Helpers are optional and use case varies application to application. base_url() is a function defined in url helper of the Framework.

You can learn more about helper in Codeigniter user guide's helper section.

You can use base_url() function once your current class have access to it, for which you needs to load it first.

$this->load->helper('url')

You can use this line anywhere in the application before using the base_url() function.

If you need to use it frequently, I will suggest adding this function in config/autoload.php in the autoload helpers section.

Also, make sure you have well defined base_url value in your config/config.php file.

This will be the first configuration you will see,

$config['base_url'] = 'http://yourdomain.com/'; 

You can check quickly by

echo base_url();

Reference: https://codeigniter.com/user_guide/helpers/url_helper.html

Dheeraj Thedijje
  • 1,053
  • 12
  • 19
0

Question -I wanted to load my css file but it was not working even though i autoload and manual laod why ? i found the solution => here is my solution : application>config>config.php $config['base_url'] = 'http://localhost/CodeIgniter/'; //paste the link to base url

question explanation:

" > i had my bootstrap.min.css file inside assets/css folder where assets is root directory which i was created.But it was not working even though when i loaded ? 1. $autoload['helper'] = array('url'); 2. $this->load->helper('url'); in my controllar then i go to my

  • I'm afraid your post is not an answer. Please start a new question using this guide : "How do I ask a good question?" here : https://stackoverflow.com/help/how-to-ask – Guillaume Raymond Jan 10 '20 at 07:51
  • Thanks, Guillaume RAYMOND, for letting me how to do best search,how should i include my question with answer, I have given best of my answer what did work for me .Thankyou for being here! – maneesh kumar gautam Jan 14 '20 at 05:13
  • You're welcome, please use this page to ask your question: https://stackoverflow.com/questions/ask and then if you think you have the answer add the answer to question like you did here. Then let me now by placing a comment here and with other moderator we will vote to clean up this current post. – Guillaume Raymond Jan 14 '20 at 07:36
0

I encountered with this issue spending couple of hours, however solved it in different ways. You can see, I have just created an assets folder outside application folder. Finally I linked my style sheet in the page header section. Folder structure are below images.

enter image description here enter image description here

Before action this you should include url helper file either in your controller class method/__constructor files or by in autoload.php file. Also change $config['base_url'] = 'http://yoursiteurl'; in the following file application/config/config.php

If you include it in controller class method/__constructor then it look like

public function __construct()
{
    $this->load->helper('url');
}

or If you load in autoload file then it would looks like

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

Finally, add your stylesheet file. You can link a style sheet by different ways, include it in your inside section

-><link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css" type="text/css" />

-> or

<?php

    $main = array(
    'href'       => 'assets/css/style.css',
    'rel'        => 'stylesheet',
    'type'       => 'text/css',
    'title'      => 'main stylesheet',
    'media'      => 'all',
    'index_page' => true
    );


echo link_tag($main); ?>

-> or

finally I get more reliable code cleaner concept. Just create a config file, named styles.php in you application/config/styles.php folder. Then add some links in styles.php file looks like below

<?php
 $config['style'] = array(
    'main' => array(
        'href'       => 'assets/css/style.css',
        'rel'        => 'stylesheet',
        'type'       => 'text/css',
        'title'      => 'main stylesheet',
        'media'      => 'all',
        'index_page' => true
    )
 );

?>

call/add this config to your controller class method looks like below

$this->config->load('styles');
$data['style'] = $this->config->config['style'];

Pass this data in your header template looks like below.

$this->load->view('templates/header', $data);

And finally add or link your css file looks like below.

<?php echo link_tag($style['main']); ?>
Asraful Haque
  • 1,109
  • 7
  • 17
0

This All Directly Access Function Will Be Loaded Through Helper Class Only.

Like URL, Security, File all Are Helpers and You can Also Load Custom Helpers.

config/autoload.php

$autoload['helper'] = array('url', 'file', 'authorization', 'custom');