Controller Vs Ccontroller

Hi! I am beginer. I don’t understand difference between Controller and CController classes. In demo projects (it is adhered to yii distributive) there is helloworld project with CController class and with code:




class SiteController extends CController

{

	/**

	 * Index action is the default action in a controller.

	 */

	public function actionIndex()

	{

		echo 'Hello World';

	}

}



But in my second project which I create via command line (like that) there is Controller class:




class SiteController extends Controller



In second project it is impossible to use CController without error message. So I am confused. Help me and explain me difference!

Controller is just a children class of CController. In test application it’s used to provide additional variables into all controllers derived from Controller. For instance, it’s used for setting of $breadcrumbs, $layout and menu $variables for layout view file.

It’s a good practice to extend YII base classes (starting with ‘C’ symbol) in your project - base classes wouldn’t be touched on YII framework updates and code would be more decoupled.

Hi egorpromo

It depends of your reusability

You can extend the component Controller (that extends core CController)

or extend the core CController directly.

therefore you can write your own Controller (resided in protected/component folder) that inherit the Controller (or CController) having extra useful fields/functions without touching the core or default class :)

Thank for reply. I want ask you some questions.

Why I need $breadcrumbs, $layout and $menu variables?

And where in documentation description of Controller class?

You need them because default layout files need them. Also - standard Gii generators assume there is $breadcrumbs attribute… If you rewrite layouts and generated files (or Gii templates) - you can get rid of those additional attributes.

Do I touch core class if I create inheritance like




class MyClass extends SomeClass

{


}



? I don’t understand you.

What are usefull fields/functions and are these for?

You should use only if needed, but it facilitates the development as they already are implemented in the framework core.

Controller Class DOC: http://www.yiiframework.com/doc/guide/1.1/en/basics.controller

You can inherit all of the core class (SomeClass like core Controller) having all the default functionality and variables - constants with additional specific needs for your web application

Imagine all your controllers need some common functions and same variables (with different values).

If you not use inheritance you should add all those extra functions and members into each Controller.

This is not reusable and maintable, so the common functions/variables can be resided into your Component/yourMainController and all controllers extend it

You’re probably getting the errors because your view files (views/layouts/*) are referencing public fields defined in Controller.php. By default, a Yii application uses the $breadbrumbs & $menu properties of Controller.php. If you change your controllers to extend CController, your view files will be trying to access properties that don’t exist.

I find extending Controllers especially useful when working with modules. For example, an Admin module.

By default, I’d like all controller actions (view, update delete etc) to be restricted to Admin users. I don’t want to redefine the access control in each Admin controller so I define a parent AdminController - this extends Controller.

Then, in my admin controllers, I extend AdminController - this allows all my controllers access to that functionality we’ve coded - once.





<?php


/**

 * AdminController class file.

 *

 * @author Matt Skelton

 * @date 1-Dec-2012

 */


/**

 * Provides additional functionality to the Admin controllers. By default it, restricts all

 * Admin actions to administrators and builds an admin menu.

 */

class AdminController extends Controller

{

    /**

 	* @return array action filters

 	*/

    public function filters()

    {

        return array(

            'accessControl', // perform access control for CRUD operations

        );

    }


    /**

 	* Specifies the access control rules.

 	* This method is used by the 'accessControl' filter.

 	* @return array access control rules

 	*/

    public function accessRules()

    {

        return array(

            array('allow',

                'users' => array('@'),

                'expression' => 'Yii::app()->user->isAdmin'

            ),

            array('deny', // deny all users

                'users' => array('*'),

            ),

        );

    }

    ....






<?php

class ProductController extends AdminController

{

    // Now, I don't need to redefine my accesscontol, but I could if I wanted to

    ...



Controller.php is implemented as a courtesy to get you started with the basic application. Look at the public fields it exposes and decide if you need them in your application. If the answer is yes, then extend Controller. If the answer is no, then extend CController.

In my case, I did want to use the breadcrumbs and menu so I extended Controller. Hope that makes sense.

http://www.yiiframew…sics.controller

Matt

Many thanks! Your explanations very useful and I learn it. I was beginner recently. Now I feel myself more experienced. :)

1 Like