Codeigniter tutorial

Codeigniter Tutorial To Create Multiple Views For Beginners

Views: 845

What are Codeigniter views?

In codeigniter or any other MVC (Model View Controller) a view is the most important part of application in user prospective and many other senses. View is how your application portraits the data or content of your web application in front of users, search engines and more. A view is basically the HTML generated after your application is processed by codeigniter.

A codeigniter view file contains HTML elements and other client side scripts like Javascript, css, UI assets and more. If you have missed the previous chapter please go through that and also download the sample files in that chapter.

 

How a view is loaded in codeigniter

In codeigniter a view is loaded using controllers, whenever you create a controller it extends the CI_Controller class to add views, the default index view of a controller is loaded using the index function. In codeigniter controllers views are nothing but functions.

In previous tutorials we learned how to create view, how to break view in parts and how to load views. Now in this tutorial we will create multiple views according to our application needs.

Codeigniter Tutorial To Create Multiple Views For Beginners

Codeigniter Tutorial To Create Multiple Views For Beginners

Creating codeigniter multiple views tutorial

Looking towards previous tutorials, we have our assets, our view parts for home and our home controller ready. So now open your views directory, create a new blank php file name it about_us.php. Add the following html inside about_us.php.

<h1>About Us</h1>

<p>This is a text for about us page, I am learning codeigniter tutorial and creating views with codeigniter is real fun</p>

Now save and close the file and proceed to controllers directory and open Home.php controller with your text editor. Add a new function public about_us after the index function. Open and close curly braces, inside the curly braces write the following codes:

$this->load->view(‘header’);

$this->load->view(‘navigation’);

$this->load->view(‘about_us’);

$this->load->view(‘footer’);

Check below for sample of whole function:

public function about_us()

{

$this->load->view(‘header’);

$this->load->view(‘navigation’);

$this->load->view(‘about_us’);

$this->load->view(‘footer’);

}

Now open your browser and go to your codeigniter installation address followed by /index.php/home/about_us i.e. for me the url is : http://localhost/codeigniter/index.php/home/about_us If you saw our newly created then congratulations you have created multiple views for codeigniter, next we will see how can we create multiple controllers in codeigniter.

Creating codeigniter multiple views tutorial

Creating multiple controllers in codeigniter tutorial

Creating your second or infinite codeigniter controllers is just like creating the first controller, open your controllers directory inside the application directory of your codeigniter installation. Create a new file and name it Contact_us.php, open the file you just created with text editor and add the following lines of code.

Creating-multiple-controllers-in-codeigniter-tutorial

<?php

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class Contact_us extends CI_Controller {

public function index()

{

}

}

Now open your views directory inside application directory and again create a blank file name it contact_us.php, now open contact_us.php inside views directory with your favourite text editor and add some html to it. I have written some html for your convenience copy paste that if needed.

<h1>Contact Us</h1>

<p>Contact me</p>

<p>Address Line 1</p>

<p>Address Line 2</p>

<p>City</p>

<p>State</p>

<p>Zip</p>

Now open the Contact_us.php controller, inside the index function write following codes:

$this->load->view(‘header’);

$this->load->view(‘navigation’);

$this->load->view(‘contact_us’);

$this->load->view(‘footer’);

Creating multiple controllers in codeigniter tutorial

Done? Now open your browser and load your Contact_us controller i.e. your codeigniter installation directory followed by /index.php/contact_us will be the location of your contact_us controller. You must see your contact us page. For me the URL is:

 

http://localhost/codeigniter/index.php/contact_us

Now we will set up the navigation for our controllers and views of codeigniter

We have done many things in this tutorial but our navigation links point to a single page that is our home page. So we will have to have a correct navigation system for our application. So now open your navigation.php And set the following URL’s.

  • For about_us set URL : <a href=”<?php echo base_url()?>index.php/home/about_us”>
  • For contact_us set URL : <a href=”<?php echo base_url()?>index.php/contact_us”>

Note: codeigniter URL structure is : codeigniter_base_url/index.php/controller_name/function_name/parameters

Now reload your page and check the navigation. If everything worked fine and navigation links are taking you to correct pages then we have completed this tutorial part else check what mistakes you made and rectify them. I have left the privacy policy URL so you can create by your own sample files are attached download them if needed. Feel free to suggest or ask any questions you have, either comment or contact us.

Sample Files

We can remove index.php from URL and set custom SEO friendly URL’s using various codeigniter methods. We will learn those methods in our upcoming tutorial: Codeigniter URL Routing, Defining Routes And htaccess To Remove index.php

Comments: 2

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