Table of Contents
Loading View Pages into the Controller
In our previous article, we have created pages in CodeIgniter. But we don’t write anything directly on the Controller section except PHP codes, and we have to write those HTML section in View part.
Creating a View Page
- Just open your CodeIgniter View folder.
- There can see two files, which are default loading files.You might delete these files,
- Here we have to create our View file.
- So create a PHP page or HTML page. You need PHP page for loading PHP scripts on the view section.
- Here I create a PHP file with the name ‘registration.php’.

You can write any HTML, PHP, CSS, JavaScript codes inside this page.
Let us write some basic HTML tags:
<html> <head> <title>Registration</title> </head> <body bgcolor="red"> Register: </body> </html>
How to load a View Page to a Controller Page?
It is very easy to load a view page to the controller.
- Just open your controller file, there we have already written many pages(functions).
- Now we are going to load our View page to our controller register page.
- So just write down below code inside the ‘register()’ page function$this->load->view(“registration”);
This line means that, this controller page is loading a view page named registration that saved on view folder.

Let us run the page register:
Then result should like this:

Like this, we can create corresponding view page for ‘login’.
Then you have to connect to the login controller
$this->load->view(“login”);
Here the name of view page should be ‘login’. That may be login.php or login.html, if both are exist, then you need to specify its format like $this->load->view(“login.php”);

Note:
In your project, there may be hundreds of view pages. So saving all view pages in a single folder is difficult. So you can categorize view files as folders.
For example:
Create a folder to keep all view files related to user and creating another folder for keeping all view files related to admin. (where both user and admin are the major modules in the project)
Now I just put my login.php into admin folder and registration.php into user folder.

So, in your controller, you must specify the name of folder with view file.
<?php class User extends CI_Controller { public function index() {
}
public function login()
{
$this->load->view(“admin/login”);
}
public function register()
{
$this->load->view(“user/registration”);
}
}
Loading Multiple View pages
In a large project, your header and footer sections will be same in all your pages. Then you can make them common. If you make a separate files for header and footer then you can load those file together with your main file.
Here are 3 files.
(You can write anything on these files)
header.php
<html> <head> <title>Registration</title> </head>
login.php
<body bgcolor="#53aaac"> Login here
footer.php
<hr/> <footer>This is the page footer</footer> </body> </html>
Now your login controller can load all these files together
public function login()
{
$this->load->view("admin/header");
$this->load->view("admin/login");
$this->load->view("admin/footer");
}
Then the out will be combination of all these view files.

We will learn more in coming lessons…
Be First to Comment