A simple MVC PHP CodeIgniter Example

In this article, we are going to create a simple example based on PHP CodeIgniter MVC based example.
Submitted by Jyoti Singh, on January 27, 2018

​In our last article, we have learnt about PHP CodeIgniter. Now,in this article, we will create a simple view page using html and will open that view by using a ​​controller. So, let’s start.

1. Create a view page

​First, we will create a view page which will be shown to the user. To do this open your CodeIgniter folder (placed in wamp/xampp folder) → open applications, here you will see multiple folders, but as we have to create a view we will open views folder.

​In this folder, make a file named "Mypage.php" save this file and write the following code in it.

codeigniter pHp example

2. Create a controller file

​Now,we will create a controller file to open our view page.

For this, create a file named "MyController.php" in controller folder.​ ​Write this code to the file.​​

Note: Your class name and file name must be same.

codeigniter pHp example

​Controller makes your work easy in CodeIgniter.​ ​Here, line 2 is a CodeIgniter syntax line you don’t need to know about this for now just copy it from CodeIgniter by default controller named Welcome.php. Line 4 represents your class named "MyController" which extends CI_Controller to access the methods defined in CI_Controller class. Line 7 has a function named index() whenever a request will come to this function, it will load a view page named "MyPage".

3. Run the File

​To run the file type this URL in your browser: "http://localhost/democodeigniter/index.php/Mycontroller/" or "http://localhost/democodeigniter/index.php/Mycontroller/index"

You should see something like this:

Output - codeigniter pHp example

Yeah! You just made your first page in CodeIgniter.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.