csdataloaderfilter Provides data loading features in filters of Controller

  1. Requirements
  2. Usage
  3. Installation
  4. Forum

This extension is very simple but enables you to use powerful programming pattern similar to what is available in Ruby On Rails. It is rather methodology and approach to controllers data flow instead of just simple extension.

Requirements

any Yii (tested with 1.13)

Usage

Example usage:

class MyController extends Controller {
   protected $model;

	public function filters() {
		return array(
			array( 'CSDataLoaderFilter', 'loadModel', 'on'=>array( 'action' ) ),
			array( 'CSDataLoaderFilter + viewModel2', 'loadModel2' ), //specify action filter default way
			'accessControl',
		);
	}
 
	public function accessRules() {
		return array(
			array( 'allow',
				'roles'=>array( 'allowEdit' => array( 'model'=>$this->model ) 
				'actions'=>array( 'action' ),
			),
			array( 'deny' ),
		);
	}
	
	public function loadModel() {
		$this->model = Model::model()->findByPk( Yii::app()->request->getParam( 'id' ) );
		if( $this->model === null ) {
			throw new CHttpException( 404, 'Model not found' );
		}
	}

	public function actionAction() {
		//you may use $this->model safely here - it is gauranteed that it is loaded, exists and user has privileges to run this action for loaded model
		...
	}

Now in details. Assume you have controller which runs some action against data models referenced with some id in URL, like: index.php?r=controller/action&id=123.

  1. first you define controller attribute that will hold loaded model. It is easili accessible also from views.

  2. then you define filters(). It is important to know, that filters are used in the same order they are defined in this method. This means you can load all required models and finally run authorization process to check if user is allowed to access them.

  3. Access rules are defined as usual except that now you can reference loaded model as parameter to access rules (it is guaranteed that it is loaded or loadModel would throw exception). Passing attributes is crucial to properly use bizRules in authorization items.

  4. data loader method - it is required that it must be "public" method (accessible from filter). You may implement scenarios when model is required (throw exception if it does not exists) or just optional models (leave null value in $this->model attribute)

  5. you code actions as usual except that you do not need to load data in the beginning and check privileges - this pattern assures you that those checks are already done.

as you can see - it is extremely easy to implement and greatly clarifies controller code. Also makes possible to use standard access rules but with parameters needed for bizRules.

in data loader filter you may specify actions that must match:

"on"=>array( 'action', 'that', 'must', 'match' )

and exclusions:

"except"=>array( 'action', 'that', 'must', 'not', 'match' )

you can also use default filters to specify on which actions data should be loaded:

array( 'CSDataLoaderFilter + viewModel2', 'loadModel2' ),
array( 'CSDataLoaderFilter - viewModel2', 'loadModel' ),
...

you may also pass additional parameter to dataloader method with:

"data"=>'parameter'
...
public function loadModel( $param ) {
     ...
}

Installation

just unzip provided class in 'components' folder so it will be accessible with just 'class'=>'CSDataLoaderFilter'

Forum

For discussion about this approach and extension, please visit forum topic: http://www.yiiframework.com/forum/index.php/topic/44781-csdataloaderfilter-topic/

2 0
3 followers
296 downloads
Yii Version: Unknown
License: BSD-2-Clause
Category: Others
Developed by: Maciej Liżewski
Created on: Jul 2, 2013
Last updated: 10 years ago

Downloads

show all

Related Extensions