use of base_url function in codeIgniter4

This function return base url or url upto root for a project, and it is very useful to add external css files links, to add external java script files, display images from public folder and create links that are in route files.

Set base_url value

  • Visit your project
  • open app folder
  • next open Config folder
  • then App.php page
  • set base_url value according to your project.
for example we have a project of name blog, so base_url will be.
public $baseURL = 'http://localhost/blog/';

Create link of assets files and images from public folder

First of all make sure, in your project public folder, you have created sub folders 
images, js, css to keep external files there.

CSS link

<link rel="stylesheet" href="<?=base_url('css/main.css')?>" >

JS link

<script src="<?=base_url('js/main.js')?>"></script>

Image file link

<img src="<?=base_url('images/flower.jpg')?>" width="200" height="200" />
© 2016 - 2022, All Rights are Reserved.