Codeigniter: Base_url doesn't seem to be worki

2019-03-02 07:59发布

问题:

I have developed a simple site that fetches tweets from the Twitter public timeline, caches them for 60 seconds and so on. I have recently moved hosts from Hostgator to Mediatemple and my site was previously working fine on Hostgator.

My application doesn't use a database connection, nor does it use any form of flat-file database either. Tweets are cached in an XML file stored in the root directory, not that most of that is important.

The url helper is being included as can be seen below (this is my helpers line from autoload.php): $autoload['helper'] = array('url');

I have also removed my index.php file from the URL using .htaccess directives and once again this was previously working on Hostgator (see my .htaccess code below):

RewriteEngine On
RewriteRule ^(application) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

In my home.php view file which is in the views folder inside of application, I am using the function base_url() which was previously working on Hostgator inside of my view files and appending it a base href value in my header: <base href="<?php echo base_url(); ?>" />

Here is what my base_url value looks like in the config.php file:

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

Although what appears to be happening is that the base_url is not to be seen at all. It doesn't appear to be echoing out the value of base_url as it appears to be empty for some reason.

What makes things weirder is that I have a link in another view file called 'fetch.php' and for some reason it appears to be stripping out the value (XSS filtering is off): <a href="threetune/show"><img src="assets/images/cookie.jpg" /></a> The threetune/show part is not to be seen and I only see an empty href value like this <a href=""><img src="assets/images/cookie.jpg" /></a>

Can anyone possibly see anything wrong that I may have done, some kind of Mediatemple server limitation or PHP.ini flag that needs to be set? Thank you and I hope I was descriptive enough.

回答1:

I am on the grid as well. After re-reading your question and doing a little testing (it works for me), I think what's happening is you forgot to load the url helper.

If you want the sites base url available to you to use in the application via base_url(), you need to load the url helper first. Either via config/autoload.php or load it manually in your controller.

Otherwise you could use config/constants.php to define constant variables:

define('BASE_URL', 'http://site.com');
define('BASE_PATH', '/path/to/CI/folder');


回答2:

Just a quick note on base_url - might be a good idea to dynamically set this variable, as it will allow your site to support multiple domains.

(i.e. $config['base_url']    = ‘http://’.$_SERVER['HTTP_HOST'])

http://thecodeabode.blogspot.com/2010/10/codeigniter-supporting-multiple-domains.html expands a bit on this by giving scripts that take into consideration protocol and port.