How to Create Pages in Codeigniter: A Quick and Easy Guide

Create Pages in Codeigniter

In this article, we’ll see Create Pages in Codeigniter.

CodeIgniter, a powerful PHP framework, empowers developers to build robust web applications quickly and efficiently. One fundamental aspect of any web application is the create pages in Codeigniter that provide unique content and functionality.

We will explore the process of create pages in CodeIgniter, from setting up routes to designing views and integrating them with controllers. By the end, you’ll have a solid understanding of how to create pages in codeigniter that bring your application to life.

Create Pages in Codeigniter

The followings are steps to Create Pages in Codeigniter:

1. The first thing in the creation of a static page is setting up the controller

  • For this, you have to create a file pages.php in CodeIgniter/application/controllers i.e. the path of pages.php will be CodeIgniter/application/controllers/pages.php.
  • Write the following code inside the pages.php file:
class Pages extends CI_Controller
{
public function view($page=‘home’)
{
}
}
  • Here we have defined a class named Pages that extends CI_Controller. CI_Controller is an inbuilt application controller class located in the CodeIgniter/system/core/Controller.php file.
  • Since the Pages class extends CI_Controller class, it can inherit the methods and variables defined in CI_Controller class.
  • Next, the class Pages has a function view() defined in it with one parameter that takes a page as its parameter.
  • The controller will now become the center of your web application. It will be considered as a super object and will be denoted as $this just like a class in PHP.

2. Next step is to create two views (common pages to all other pages) i.e. two-page templates. This is one of the important steps for Create Pages in Codeigniter.

  • The template is a logic that is written once and can be used a number of times.
  • We will create two PHP pages, one for header and another for footer that can be used as templates in the web application.
  • For this, you need to create a folder template in the CodeIgniter/application/views, so that your common files remain at one place in the templates folder.
  • Now create a file header.php inside the templates folder and write the following code in it:
<html>
<head>
<title>Static Pages Tutorial</title>
</head>
<body>
<h1>Welcome to <?php echo $title; ?> of Static Pages Tutorial</h1>
  • Then after this create a footer.php page inside templates folder and write the following code in it:
</body>
<footer>

<em>&copy; 2017</em>
</footer>
<html>
  • Here our header and footer are ready to use with any other page.
  • We have the HTML head section with the title on the header.php page. We have also started the body section on the header.php page with a statement that welcomes the user to the particular page opened.
  • Next, we have a footer defined in the footer.php page and closed the body and HTML tag.
  • The common things have been done, now we need to create the body part of the page.
  • For this create another folder named pages in CodeIgniter/application/views folder.
  • Inside this page, the folder create a file home.php which will contain only the body part of the homepage.
  • Write the following code in the home.php file:
<br><br>
<h2>Welcome to CodeIgniter</h2>
<br>
<h3>Learn CI.</h3>
  • Here we have just coded the body part of the home.php page. The body tag of HTML has been started on the header.php page, so directly the content is written on the home.php page.
  • You can create any number of pages in this page’s folder that you want in the web application.

3. Editing the pages.php file in CodeIgniter/application/controllers.

  • We have now finished with the templates and the body of the homepage. So to assemble all these pages we have to approach the controller which is the pages.php file in our case.
  • So open the pages.php file and append continue writing the following code:
<?PHP
class Pages extends CI_Controller
{
public function view($page=‘home’)
{
if(!file_exists(‘application/views/pages/’.$page.‘.php’))
{
echo “Sorry, file does not exist”;
}else{
$data[‘title’]=ucfirst($page);
$this->load->view(‘templates/header’,$data);
$this->load->view(‘pages/’.$page,$data);
$this->load->view(‘templates/footer’,$data);
}
}
}
?>
  • We had set up a controller with a view() method. But now we want to display the page we have created.
  • The pages should be displayed in the header, home, footer order so that the header section come at the top, the home page body in the middle and the footer at the bottom.
  • So in the view() method we take the page to be displayed as the parameter, then we checked to see if the home.php file exists in the pages folder using the in-built file_exists() function as shown below:
if(!file_exists(‘application/views/pages/’.$page.‘.php’))
  • Here if the desired page exists, it will be displayed otherwise error message will be displayed.
  • If the page exists, it is displayed using the following code in the else part:
else
{
$data[‘title’]=ucfirst($page);
$this->load->view(‘templates/header’,$data);
$this->load->view(‘pages/’.$page,$data);
$this->load->view(‘templates/footer’,$data);
}
  • Here we have passed the page in our $page parameter to the in-built ucfirst() function which capitalizes the first letter of the string and stored in the $data array with key title.
  • Now the $data array contains the title of the page with its initial letter capitalized as the value of key title.
  • Now next step is to display the header page. This is done using the statement,
$this->load->view(‘templates/header’,$data);
  • Here, we have the path of header.php page and the $data array as parameters of the view() function.
  • We had used the title of the page in the header.php page such that whichever page is opened, it will show its title using statement
    .
  • So in the second parameter of view() function $data array is passed to the header page to make the title available.
  • Remember the values stored in $data array are assigned to the variables with the names of their keys. I mean the $data[‘title’] in controller will be accessed as $title in view.
  • Then after the header, the home and footer is also loaded similarly.

4. Next step is to see your page in the browser.

  • Before browsing your page let us understand something about the URL supported by MVC architecture.
  • The MVC architecture supports the following URL:

http://domain_name/[controller-class]/[controller-method]/[arguments]

  • So we will browse our page in the similar manner.
  • Open your browser and type the following address in the address bar:

http://localhost/CodeIgniter/index.php/pages/view

  • In the above address:
  • http://localhost -> is your localhost machine.
  • CodeIgniter/ -> is the CodeIgniter folder on your server
  • index.php/ -> is the default page in CodeIgniter
  • pages/ -> is your controller-class name
  • view/ -> is your controller-method name

5. If instead of writing such a long address, you want your homepage to be loaded automatically, then do the following steps:

  • Go to CodeIgniter/application/config folder and open routes.php file with notepad/notepad++.
  • The routes.php file contains the following statement on line number 41:
$route[‘default_controller’] = “welcome”;
  • Here the default controller given is welcome. We need to replace the default controller to the controller and method separated by forward slash as shown below:
	
$route[‘default_controller’] = “pages/view”;
  • This will set our web application controller as default controller as we have assigned the $route’s default_controller parameter to our pages controller-class followed by the view controller-method.
  • Save the routes.php file after the changes and close it.

Now just open the browser and write the following address in its address bar:

http://localhost/CodeIgniter

Still the output with the above address is shown below which allows auto-loading of our web application.

That’s it. This is how we Create Pages in Codeigniter. Thanks!

Create pages in CodeIgniter opens up a world of possibilities for building interactive and engaging web applications.

By leveraging the framework’s routing system, controllers, views, and navigation mechanisms, you can create pages in codeigniter that deliver personalized content and provide a seamless user experience.

Remember to follow best practices, modularize your code, and explore CodeIgniter’s extensive documentation and community resources to maximize your development potential.

With CodeIgniter’s power at your fingertips, you’re well-equipped to craft exceptional dynamic pages that bring your web application to life.

I hope this article helps in Create Pages in Codeigniter.

One thought on “How to Create Pages in Codeigniter: A Quick and Easy Guide

Write a Reply or Comment

Your email address will not be published. Required fields are marked *