Step 1.2: Again about MVC and CodeIgniter – The views

Created at: January 21, 2015; Last update: January 21, 2015

(created: January 21, 2015; last update: -)

In the previous tutorial we worked a bit with the controllers. Now, remember how I told you that when talking about MVC patterns, the controllers send the data to the views so that the data would be outputted to the browser?

Well, now we will talk a bit about these views. In particular, we will talk about the view that is called by the Welcome controller, “welcome_message“.

The views can be found in application/views directory, so we will find the welcome_message.php in there.

We will see a plain page if we look inside it… so, let’s delete everything and just create a basic html structure:

Now, if in the controller we’ve defined some variables (and we did), how would we go about using them in the view? As I already mentioned in the previous tutorial, when loaded from the controller, the view can receive variables inside an array:

Inside the view, the variables can be accessed by calling the keys of the array as variables. So, if for example we have an array like this one:

…when we will be inside the view we can access those keys by simply translating them into variables:

Let’s put what we’ve learned into practice. As you remember, in the controller we’ve defined a page title ($data[‘page_title’] = ‘Our first CodeIgniter application’; ). Now in the view we simply replace the hard-coded page title with the variables sent by the controller:

Simple, isn’t it? Now how about the models? Well… for that I have another (older) tutorial. Hope you will enjoy it.

7 thoughts on “Step 1.2: Again about MVC and CodeIgniter – The views

  1. Jacky

    I followed the step 1 and when trying to run the http://localhost, it seems something is broken
    ==========================
    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: page_title
    Filename: views/welcome_message.php
    Line Number: 76
    Backtrace:
    File: C:\wamp\www\application\views\welcome_message.php
    Line: 76
    Function: _error_handler
    File: C:\wamp\www\application\controllers\Welcome.php
    Line: 24
    Function: view
    File: C:\wamp\www\index.php
    Line: 292
    Function: require_once
    =========================

    it seems the View is not getting the variable declared at the Controller. What did I missed? I’m using WAMP with PHP 5.5

    Reply
  2. Jacky

    Hi Avenir

    First of all. Thanks for this tutorial!! You have gone to great length to help people in learning CI

    I did missed something…took me forever to look. Used to be VC++ dev; too used to the IDE function to help me out and using Notepad++ for PHP & CI – certainly all the ugliness are coming out LOL

    I missed adding the $data to the load view method.
    $this->load->view(‘welcome_message’,$data );
    Good lesson learn

    Reply
    1. avenirer Post author

      Well… you make me jealous for knowing C++… Just for the sake of discussion why would you move from C++ to PHP and not to .NET?

      Reply
      1. Jacky

        Hi Avenir

        C++ is a very strong language and sometimes it drives me nuts 😉 So don’t be jealous LOL
        IMHO, .Net has a limitation of being too heavy. Overly heavy in fact and hosting server must be IIS (yay?). All the components are tightly coupled and their associated costs can be problem, not to mention these components are never known to be resource friendly. Costs and maintenance is a major issue with .Net. Then there’s a problem where you’re locked down with a one framework only.

        PHP’s components on the other hand are very loosely coupled. From servers down to associated framework are all loosely coupled. I have the option to choose what framework i use for which project and switching from one framework to another doesn’t carry a steep learning curve – once you get the concept and language right.

        I’m happy to be corrected, but that’s just my view 🙂

        Reply
  3. Jijin

    Till this page u where on my adblock list,
    but from this page on u are white listed on all blocking+popup extension,
    i have to say that , yout tutorial is awesome piece of an art , i would recommend all beginner in codeigniter to learn from this tutorials ,
    writer, keep doing , awesome work man, thanks again, i will keep visit ur site, 🙂

    Reply

Leave a Reply

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

No spam? * Time limit is exhausted. Please reload CAPTCHA.