is it worth it to use base_url() for every clientside inclusion?
I us开发者_Go百科e CodeIgniter and i love it, but i don't know whether it's really worth it to do like this:
<link rel="stylesheet" href="<?php echo base_url(); ?>css/main.css" />
<script src="<?php echo base_url(); ?>js/jquery.js"></script>
<script src="<?php echo base_url(); ?>js/functions.js"></script>
...
<img src="<?php echo base_url(); ?>images/dolphin.png" />
Rather than just:
<link rel="stylesheet" href="/css/main.css" />
<script src="/js/jquery.js"></script>
<script src="/js/functions.js"></script>
...
<img src="/images/dolphin.png" />
The first method adds a lot of weight to the page but it's reliable when you decide to use the same app in a subfolder and such.
Which one should i go with?
If you think you might need to move the app to other subfolders (and not other sub domains), it probably is worth using <?php echo base_url(); ?>
, however if you can assume that the app will always be installed on it's own domain or sub domain definitely do away with the function call, it adds unnecessary clutter and sends more to the users browser.
It's down to what you think your application will need to do.
In my opinion you should always use base_url() for defining your path because like that you will make sure that your path will always be the right one. If you think that echo function is too dirty, you can always use Template engines like Code igniter built in template parser class or some external like Smarty.
Just thought I'd add some things that has been relevant for me.
As you've mentioned, if you ever need to install Codeigniter in a subdirectory, the
/
leading forward slash will of course not work. You would have to include the subdirectory name in the path. Personally, this comes up a lot because we'll install redesigns or prototypes in sub-directories. However, this can be nice (if it's applicable) for switching to SSL if you aren't already letting CI auto-detect your base url (as of v2.0.2).Changing your
$config['base_url']
to not include the full domain can be a bad idea. Off the top of my head, this would break links and references in emails sent by your application that use thebase_url()
function, and in general is likely to cause unexpected results.Almost every HTML tag you need to use your base url in, like
<link>
<img>
and<a>
, is covered by a Codeigniter function. (link_tag()
,anchor()
,img()
). These will take care of the base url for you. (Why they left out<script>
is beyond me...)
However, I agree - using the full base url adds a lot of unnecessary page weight, especially in your navigation. Here's what I do to grab the path (in case of sub-directory installs):
// constants.php
$base_uri = $_SERVER['SCRIPT_NAME']; // Always index.php (bootstrap), right?
$base_uri = str_replace('index.php', '', $base_uri);
define('BASE_URI', $base_uri);
You can change this to a function or config item or whatever, I prefer a constant. Then you can use:
<script src="<?php echo BASE_URI; ?>js/functions.js"></script>
This will usually be a long-winded way to say /
, but handles the sub-directory issue.
This might seem like a waste of time, but when you have lots of installations using the same codebase, less configuration is better.
As a compromise between readability and flexibility I would do the following:
<link rel="stylesheet" href="<?= site_url('css/main.css') ?>" />
<script src="<?= site_url('js/jquery.js); ?>"></script>
<script src="<?= site_url('js/functions.js); ?>"></script>
...
<img src="<?= site_url('images/dolphin.png'); ?>" />
精彩评论