Yii Framework Forum: Does Anyone Want A Tutorial On Observableactions? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Does Anyone Want A Tutorial On Observableactions?

Poll: ObservableActions Poll (11 member(s) have cast votes)

Is this a good pattern for customizing actions at runtime?

  1. Yes (5 votes [45.45%])

    Percentage of vote: 45.45%

  2. No, there's a better way (0 votes [0.00%])

    Percentage of vote: 0.00%

  3. No, it violates XYZ principle (please explain) (0 votes [0.00%])

    Percentage of vote: 0.00%

  4. I'm not sure (6 votes [54.55%])

    Percentage of vote: 54.55%

Should I create a tutorial explaining this pattern?

  1. Yes (11 votes [100.00%])

    Percentage of vote: 100.00%

  2. No (0 votes [0.00%])

    Percentage of vote: 0.00%

Vote Guests cannot vote

#1 User is offline   danschmidt5189 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 19-February 13

Posted 05 June 2013 - 05:32 PM

Tutorial on ObservableActions?

In the course of building a RESTful API for a client, I've made extensive use of events to make actions reusable and flexible. Since event-handling in actions isn't widely discussed, I thought I'd make a tutorial covering:

  • Creating a base action class with an init() method and some predefined events (e.g. onAfterConstruct(), onAfterInit())
  • Show how to create custom action events and attach handlers in your controller
  • Build an example GetRecordAction
  • Use event handlers to deal with 403/404/410 situations without cluttering the action code itself

I'm wondering if the community would benefit from a tutorial on this, if this is already a well-known pattern, or if there's simply a better way to do this.

Principles - Actions should be DRY/SRP

DRY = Don't Repeat Yourself
SRP = Single Responsibility Principle

We want classes that are powerful, loosely-coupled, and re-usable. Yii makes it easy to do this, but the documentation/existing tutorials don't make it obvious how to do this with respect to Actions.

Example Code

Below is an example of how the GetRecordAction class would be configured in your controller. Note that I'm using anonymous functions in the example. This is just for transparency; in real life you might want to create classes that encapsulate those checks, if only to make them easily re-usable elsewhere in your application.

// In a "BooksController" class...
public function actions()
	return array(
		// Retrieve a book from the database
		'getRecord' =>array(
			'class' =>'api.components.actions.GetRecordAction',
			'modelClass' =>'Book',
			'scenario' =>'api.get',
			'onBeforeLoad' =>function($event)
				$accessGranted = Yii::app()->authManager->checkAccess(
					array('bookId' =>$event->params['recordPk'])
				if (!$accessGranted)
					throw new CHttpException(403, "EW GETTTOUT");
			'onAfterLoad' =>function($event)
				if ($event->params['record']===null)
					throw new CHttpException(404, "The requested book was not found");
				} else if ($event->params['record']->status==3)
					throw new CHttpException(410, "That book was deleted and is no longer available");


Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users