base_url() function not working in codeigniter
Asked Answered
C

12

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>
Circumnavigate answered 23/6, 2011 at 4:36 Comment(6)
Can you show us your code, please?Bisect
@Jonathan Sampson: I have tried to use echo base_url(); in the main body section and got an empty resultCircumnavigate
@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.Bisect
Can you place <?php $this->load->helper('url'); ?> to the top of your view and try once more?Bisect
@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?Circumnavigate
@Sanks Look to application/config/autoload.php at line 67. Make sure you see $autoload['helper'] = array('url');.Bisect
B
158

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

Bisect answered 23/6, 2011 at 4:40 Comment(10)
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.Yetac
@Wesley I'm suspecting it's a missing call to echo as well.Bisect
@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.Circumnavigate
@SanksR Are you placing echo before the call or not? Please update your question with relevant code so that we can better assist you.Bisect
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 codeigniter.com/user_guide/general/alternative_php.htmlSalvo
@Salvo 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.Bisect
@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.Salvo
@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.Circumnavigate
@Salvo 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.Bisect
@SanksR To auto-load many things, just increase the array: $autoload['helper'] = array('url', 'html', 'form');. Don't redeclare $autoload['helper'] :)Bisect
P
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.

Packton answered 29/7, 2014 at 10:6 Comment(0)
A
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

Alasdair answered 2/5, 2017 at 10:11 Comment(0)
F
3

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

$config['base_url'] = 'http://example.com/';
Fakieh answered 23/6, 2011 at 4:45 Comment(0)
N
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" />
Numbskull answered 22/3, 2015 at 5:53 Comment(0)
S
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
Swanhilda answered 16/7, 2021 at 16:31 Comment(0)
B
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.

Benedict answered 17/2, 2015 at 23:36 Comment(0)
C
0

Load url helper in controller

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

Champollion answered 11/11, 2019 at 9:49 Comment(0)
Y
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

Yl answered 29/11, 2019 at 8:48 Comment(0)
L
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

Luz answered 10/1, 2020 at 7:27 Comment(3)
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 : stackoverflow.com/help/how-to-askCostotomy
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!Luz
You're welcome, please use this page to ask your question: 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.Costotomy
B
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']); ?>
Bunche answered 29/2, 2020 at 16:38 Comment(0)
M
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');
Maki answered 20/1, 2022 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.