CodeIgniter 4 - How to set the default page

Posted by Ridwan Fadilah on Jun 16, 2020 - 09:00 pm

How to set the default page

a simple way to set the default page, removing public and index from the URL in CodeIgniter 4

CodeIgniter is one of the PHP framework that can help your work. That's the first what I feel when using this framework. CodeIgniter can make a program more efficient, is like a bootstrap when you work with CSS. This tutorial will show you how to set the default page in CodeIgniter. It's the first step when we use CodeIgniter.

Remove the Public and Index from the URL

The first must to do is make sure you have open your project directory in your code editor.

After that, open the public folder in your file explorer. In this folder, you'll find the index.php file and the .htaccess file. Copy the file and paste it in your root directory.

CodeIgniter root directory

Back to your code editor, open the app.php in the config folder. Find the $baseURL variable and change the URL to your project name URL.

from this:
 public $baseURL = "http://localhost:8080/";
to this :
 public $baseURL = "http://localhost/your_project_name/";
Example :
 public $baseURL = "http://localhost/ci-4/";

If you're using mod_rewrite to remove the page, set the $indexPage variable so that it is blank.

 public $indexPage = '';
Index Blank

Same as the $baseURL you need to change the $uriProtocol variable.

 public $uriProtocol = 'PATH_INFO';
Uri Protocol

Save the configuration, and then open the index.php from your root directory. Change the $pathsPath variable.

from this:
 $pathsPath = FCPATH . '../app/Config/Paths.php';
to this :
 $pathsPath = FCPATH . 'app/Config/Paths.php';

Save all changes and try to load your page in your browser.

Set the Default Page

After removing the public and index from the URL, let's start to set the default page of your web.

With CodeIgniter, it's so easy to set the default page. We only need to change some configuration in CodeIgniter.

First, open the app/view/ folder and make a new folder named as home or whatever you want. After that, create a new file and give the name index.php in your home folder.

New Folder & File

Now you can put anything to your index.php file such as HTML element for the home page.

For the last step, open the Home.php in Controllers folder, remove the ('welcome_message') and replace with ('home/index').

from this:
 return view('welcome_message');
to this :
 return view('home/index');

The home/index defines the home folder and index.php file.

That's the way to set the default page of your website in CodeIgniter 4. If you don't understand this article, you can see the video of this tutorial on my YouTube Channel.

Thank's for reading this tutorial.


Related Articles