base_url() function not working in codeigniter

2019-01-01 07:31发布

I am developing a we application using codeigniter. I am trying to use base_url() function but it shows empty results. I have 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. Please help.

<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>

7条回答
大哥的爱人
2楼-- · 2019-01-01 07:46

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.

查看更多
浪荡孟婆
3楼-- · 2019-01-01 07:46

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

查看更多
只若初见
4楼-- · 2019-01-01 07:49

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

$config['base_url'] = 'http://example.com/';
查看更多
梦醉为红颜
5楼-- · 2019-01-01 07:57

First of all you must load the url helper file to your project

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

Then you will get base_url by

echo base_url();

Read more about base_url here

查看更多
萌妹纸的霸气范
6楼-- · 2019-01-01 08:07

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

查看更多
闭嘴吧你
7楼-- · 2019-01-01 08:10

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.

查看更多
登录 后发表回答