Published using Google Docs
Yii Quick Start 2 (BootStrap, OOP, MVC and REST)
Updated automatically every 5 minutes

Yii Quick Start 2

Introduction

Steps

Conclusion

Introduction

This tutorial explains the role of the index.php of a Yii website as a bootstrap. Bootstrap means to load a small program that eventually calls the desired program into the computer, similar to an operating system being called by a BIOS program.

This tutorial continues from http://php-steps.blogspot.com/2013/11/yii-quick-start-1.html

Steps

1) Using File Explorer browse the Yii Blog Demo root file.

2) The name index.php is a common name found in PHP websites.

The content of the file is as follows:

Line no. 4 tells the server the location of Yii Framework folder.

Line no. 5 tells the server the location of the configuration details for this website.

Line no. 6 tells the server to include the yii.php when processing the index.php

Line no. 11 tells the server to create a static Web Application object based on the given configuration (in Line no. 5). This is an Object Oriented style of programming.

3) By default, Yii Web Application Object passes the control to {webroot}/protected/controllers/SiteController.php

How does it happen? Look at the following tracing table.

File Name

Line No.Code

Extract

Index.php

13

Yii::createWebApplication($config)->run();

Yii.php

25

class Yii extends YiiBase

YiiBase.php

98

return self::createApplication('CWebApplication',$config);

CWebApplication.php

63

public $defaultController='site';

The word ‘site’ refers to {webroot}/protected/controllers/SiteController.php

4) Open {webroot}/protected/controllers/SiteController.php

The instruction “$this->render('index');” will render the view file ‘protected/views/site/index.php’ using the default layout ‘protected/views/layouts/main.php’

View File = File that feeds the content to $content element.

Layout File = File that contains the elements HTML Header, Main Menu, BreadCrumb, $content and Footer.

<?php

class SiteController extends Controller

{

        /**

         * Declares class-based actions.

         */

        public function actions()

        {

                return array(

                        // captcha action renders the CAPTCHA image displayed on the contact page

                        'captcha'=>array(

                                'class'=>'CCaptchaAction',

                                'backColor'=>0xFFFFFF,

                        ),

                        // page action renders "static" pages stored under 'protected/views/site/pages'

                        // They can be accessed via: index.php?r=site/page&view=FileName

                        'page'=>array(

                                'class'=>'CViewAction',

                        ),

                );

        }

        /**

         * This is the default 'index' action that is invoked

         * when an action is not explicitly requested by users.

         */

        public function actionIndex()

        {

                // renders the view file 'protected/views/site/index.php'

                // using the default layout 'protected/views/layouts/main.php'

                $this->render('index');

        }

        /**

         * This is the action to handle external exceptions.

         */

        public function actionError()

        {

        ...

        }

        /**

         * Displays the contact page

         */

        public function actionContact()

        {

         ...

        }

        /**

         * Displays the login page

         */

        public function actionLogin()

        {

        ...

        }

        /**

         * Logs out the current user and redirect to homepage.

         */

        public function actionLogout()

        {

        ...

        }

}

5) Refers to the file SiteController.php above

Notice that it has action methods:

6) You can call these action methods through URL call, e.g when you type http://localhost:8080/yii/demos/blog/index.php/site/contact, the method actionContact will be invoked. The method actionContact will create a form object, $model and passes the object to SiteController for render. This is also known as RESTful style (read more here, http://rest.elkstein.org/)

Conclusion

This tutorial shows how Yii uses Object Oriented Programming to implement the Model-View-Control framework.