Blog for Learning

This blog contains lesson material, material summary, questions and answers, definitions, types and examples, and others.


Powered by Blogger.


Flag Counter


Why Should We Use the base_url () Function to Insert Static Files in Codeigniter?

When I became a tutor in a web lab, at that time we studied CodeIgniter . I found a few cases, related to the insertion of asset files. Why should you use the function base_url()That is the question asked from one of the practicum participants. 

Then I answered by showing a demo. How to insert using functions base_url()and how without functions base_url()

Inserting asset files with functions base_url():
<link rel=“stylesheet” href=”<?php echo base_url(‘assets/bootstrap/bootstrap.min.css’) ?>” />
<link rel=“stylesheet” href=”<?php echo base_url() ?>assets/bootstrap/bootstrap.min.css” />

Inserting asset files without functions base_url():
<link rel=“stylesheet” href=“assets/bootstrap/bootstrap.min.css” />

Results Both can, the asset file is successfully inserted. But, it turns out my opinion is wrong. Inserting asset files without using a function base_url()will only be valid on the index (root) page . Once you open another page, the asset's URL address changes. For example, I open the URL:
Then what will happen is, the asset's URL address will change to.
Look, the asset file will never be found. Then, what if the insertion is like this.
<link rel=“stylesheet” href=”/assets/bootstrap/bootstrap.min.css” />
Note the slash (/) in front of assets . The slash tells the root of the web. While the root of the web that is being worked on is http://localhost/, then it will form a URL like this:
This way, it might work if the web has been hosted and placed in the root . However, we are currently working on localhostSo, we should use the function base_url()to insert asset files so that localhostthe hosting server can work. What do you think?

0 Komentar untuk "Why Should We Use the base_url () Function to Insert Static Files in Codeigniter?"

Silahkan berkomentar sesuai artikel

Template By Kunci Dunia
Back To Top