0

I'm developing a web application on the same domain as the "production" application -- it's just a sub directory of the top level website. Something like: www.site.com/v3/...

I've seen several people trying to get relative paths working with CodeIgniter -- in the past I had just stuck base_url() in every HTML image declaration of the "v3" site, but I bought an HTML site template recently and embedding <?php base_url() ?> in hundreds of HTML tags seems so inefficient.

Hoping someone can help with this -- I'd like to be able to transition the dev code (aka "/v3/") directly into use without searching and replacing all of those HTML references.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • As far as I know, `base_url()` is the function for creating correct paths in CodeIgniter. Therefore, if the template author didn’t use this function then they’re not as clued up on CodeIgniter best practices as they could be. – Martin Bean Feb 11 '13 at 00:43
  • Right -- however in this case, the template wasn't explicitly created for codeigniter, I just like CI and would like to continue using it. I'm trying to integrate PHP (via CI) into a HTML template... – user2059927 Feb 11 '13 at 00:45
  • Unfortunately you’re only option is to replace URLs with CodeIgniter-specific function calls like `base_url()` then. – Martin Bean Feb 11 '13 at 00:52

2 Answers2

0

This is exactly the purpose of $config['base_url'] in application/config/config.php. If you set it to http://site.com/v3/, it will be reflected across your site everywhere you use base_url() or site_url()

However, I should note that using base_url() or site_url() across your entire site for every src, href, etc is bad practice. You're invoking the PHP interpreter like mad.

A much better solution is to tell your HTML document about your base using the <base> tag: <base href="<?= site_url();?>"> in the <head>.

There you have it. You use site_url() once, and from now on you can just use:

<a href='controller/action'>My Link</a> and <img src='my_image.png'/>

In general and as an overall principle, when you find yourself duplicating yourself... find a way to remove that duplication. Keep it DRY.

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • you might want to read this first & decide: http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag – jmadsen Feb 11 '13 at 00:57
  • @jmadsen: thanks a lot for this link - was completely unaware of these gotchas. Just from personal experience, I have been using `` for years and have never run into snags. Will give this a read. Cheers. – Jordan Arsenault Feb 11 '13 at 00:59
  • 1
    no problem - I actually got this by searching for , since I didn't know about it. So I learned something from you! I'm not saying good or bad idea, just that there seem to be more things to look at when deciding – jmadsen Feb 11 '13 at 01:01
0

Thanks for all the help, folks.

After reading about the potential issues with the tag, I re-thought my approach -- I created a sub-domain of my main site (something like dev.mysite.com). That way I can work on the code exactly as it will appear on the production version, with only a base URL change in CI's config.php.

For now this seems the most simple solution, since calling the php pre-processor for every HTML tag that needs a relative path is exhausting and inefficient, and using the tag can lead to inconsistent results.