Extending common classes to allow better customization

You are viewing revision #5 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#4)

The two most commonly-used base classes in most Yii applications are CController and CActiveRecord, each extended to your own particular models and controllers.

It's good practice to create customized classes of your own early in your project, even if you don't have any particular reason to right away. This is done by creating two files in the protected/components/ folder, one for MyController.php and the other for MyActiveRecord, each of which extends its obvious class:

<?php
// in protected/components/MyController.php

class MyController extends CController {

}

// in protected/components/MyActiveRecord.php

class MyActiveRecord extends CActiveRecord {

}

Note: the common Yii convention is to remove the leading C when creating custom classes, by I believe it's more obvious that these are user-defined classes by using the My prefix. But choose the convention you prefer.

Note#2: If you're looking at the Blog demo, the application controllers already extend from protected/components/Controller.php, which is the blog's helper class of this kind - no need to extend it again.

Once these class files have been created, edit each of your controllers and models:

// in protected/controllers/PostController.php

class PostController extends MyController {
    ...

// in protected/controllers/CommentController.php

class CommentController extends MyController {
    ...

// in protected/models/Post.php
class Post extends MyActiveRecord {
    ...

// in protected/models/Comments.php
class Comment extends MyActiveRecord {
    ...

Do this with all models and controllers.

Once this is done, then the My versions of the classes will be available for common customization, and one of my favorites is granting easier access to the "flash" mechanism for displaying a message on the next refresh (note: this has nothing to do with Adobe/Macromedia "Flash", the animation/movie mechanism).

// in protected/components/MyController.php

class MyController extends CController {

    public function setFlash($key, $value, $defaultValue=NULL)
    {
        Yii::app()->user->setFlash($key, $value, $defaultValue);
    }

    public function setFlashSuccess($value, $defaultValue = null)
    {
        $this->setFlash('success', $value, $defaultValue);
    }
    public function setFlashError($value, $defaultValue = null)
    {
        $this->setFlash('error', $value, $defaultValue);
    }
    ...

Now, these helper functions — which don't do anything but save a few keystrokes — are easily available to controller actions:

// in a controller file

    public function actionSomething($id)
    {
        ... do some action
        if ( $success)
            $this->setFlashSuccess('Action completed successfully!');
        else
            $this->setFlashError('Could not complete the action');
        ...
    }

Now, the success or failure of the action will be displayed in a small banner at the top of the next refresh, disappearing after that. Many find this version easier to read than:

...
        if ( $success)
            Yii::app()->user->setFlash('success', 'Action completed successfully!');
        else
            Yii::app()->user->setFlash('error', 'Could not complete the action');
        ...

I also found that since all of my controllers used the same layout file, I was able to add it to the MyController class, allowing me to remove it from all the individual controllers:

// in protected/components/MyController.php

    ...
    public $layout='//layouts/column2';

Likewise with $breadcrumbs and $menu. The more code you can move out of individual controllers and into common base classes, the more robust the code becomes and the easier it will be for others to maintain.

You'll find many avenues of customization as you learn more about Yii, and this framework of customized key classes will make it much easier.

36 0
19 followers
Viewed: 42 204 times
Version: Unknown (update)
Category: Tips
Tags:
Written by: Steve Friedl
Last updated by: Steve Friedl
Created on: Dec 17, 2010
Last updated: 13 years ago
Update Article

Revisions

View all history