Should I use base_url () for every client inclusion?

I use CodeIgniter and I like it, but I don’t know if it really needs to be done 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" /> 

Instead simply:

 <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 is reliable when you decide to use the same application in a subfolder, etc.

Which one should I go with?

+4
source share
4 answers

If it seems to you that you need to transfer the application to other subfolders (and not to other subdomains), it is probably worth using <?php echo base_url(); ?> <?php echo base_url(); ?> , however, if you can assume that the application will always be installed on its own domain or subdomain, it certainly eliminates the function call, it adds unnecessary clutter and sends more to the users browser.

It depends on what you think you will need to do.

+2
source

In my opinion, you should always use base_url () to determine your path, as you will ensure that your path is always correct. If you think that the echo function is too dirty, you can always use template engines, such as Code igniter, built-in to the template parser class, or some external ones, for example Smarty .

+1
source

Just thought I'd add some things that were important to me.

  • As you already mentioned, if you ever need to install Codeigniter in a subdirectory, the leading slash will certainly not work. You will need to specify the name of the subdirectory in the path. Personally, this is a lot, because we will install redesigns or prototypes in subdirectories. However, it can be nice (if applicable) to switch to SSL, if you do not already allow CI to automatically determine your base url (starting from version 2.0).

  • Changing your $config['base_url'] to not include the full domain might be a bad idea. At the top of my head, this will break the links and links in the emails sent by your application that use the base_url() function and, as a rule, can cause unexpected results.

  • Almost every HTML tag you need to use your base url, such as <link> <img> and <a> , is covered by the Codeigniter function. ( link_tag() , anchor() , img() ). They will take care of the base URL for you. (Why did they leave outside the <script> outside of me ...)

However, I agree - using the full base url adds a lot of extra page weight, especially in your navigation. Here is what I do to capture the path (in case of installing a subdirectory):

 // 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 configuration item or something else, I prefer a constant. Then you can use:

 <script src="<?php echo BASE_URI; ?>js/functions.js"></script> 

This will usually be a long way to say / , but handles the problem with the subdirectory.

This may seem like a waste of time, but when you have many installations using the same code base, a smaller configuration is better.

+1
source

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'); ?>" /> 
0
source

All Articles