Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

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’) ?>” />
or
<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:
http://localhost/proyek/index.php/register
Then what will happen is, the asset's URL address will change to.
http://localhost/proyek/index.php/register/assets/bootstrap/css/bootstrap.min.css
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:
http://localhost/assets/bootstrap/css/bootstrap.min.css
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?

Reference: https://www.petanikode.com/codeigniter-base-url/
0 Komentar untuk "Why Should We Use the base_url () Function to Insert Static Files in Codeigniter?"

Silahkan berkomentar sesuai artikel

 
Copyright © 2015 - 2024 Blog for Learning - All Rights Reserved
Template By Kunci Dunia
Back To Top