2017 © Pedro Peláez
 

yii2-extension yii2-action-controller

Action controller help you code clean

image

vuongminh/yii2-action-controller

Action controller help you code clean

  • Thursday, December 22, 2016
  • by vuongminh
  • Repository
  • 1 Watchers
  • 1 Stars
  • 33 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Total Downloads License, (*1)

Action controller

It's an extension of yii\base\Action. It help you code fast, clean and easy manage actions of controller., (*2)

If your project have many actions in one controller and one action have many scenarios, maybe some difficults for managing your complex code later. This extension may help you solve that problem., (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require --prefer-dist vuongminh/yii2-action-controller

or add, (*6)

"vuongminh/yii2-action-controller": "v1-beta"

to the require section of your composer.json., (*7)

Basic usage

In your controller must extends an abstract class vuongminh\ac\ActiveController and define method listAction return an array store name of actions., (*8)

<?php
namespace app\controllers;

use vuongminh\ac\ActiveController;

class GuestController extends ActiveController
{

     public function listAction()
     {
         return ['login', 'forgot-password'];
     }   
}

After define actions name you create directory have name of controller name in @app\controllers\ and create action files have prefix "Action" . "name action", you can override prefix property $actionPrefix default is Action., (*9)

  @app/controllers/guest/ActionLogin.php
  @app/controllers/guest/ActionForgotPassword.php

In action files you define an action class like normal of yii, (*10)

<?php

namespace app\controllers\guest;

use yii\base\Action;


class ActionLogin extends Action
{
    public function run(){
        return $this->controller->render("login");
    }
}

Advanced usage

In this case your action class must be extends an vuongminh\ac\ActiveAction and you can customize layout, class, response format, model of actions., (*11)

<?php
namespace app\controllers;

use vuongminh\ac\ActiveController;

class GuestController extends ActiveController
{
     public function listAction()
     {
         return [
             'login' => [
                 'class' => 'app\controllers\customguest\Login',
                 'layout' => 'abc',
             ],
             'forgot-password' => [
                 'reponseFormat' => \yii\web\Response::FORMAT_JSON
             ]
         ];
     }   
}

Short call app component in action class:, (*12)

<?php

namespace app\controllers\guest;

use vuongminh\ac\ActiveAction;


class ActionLogin extends ActiveAction
{
    public function run(){
        $user = $this->user; // equivalent to \Yii::$app->user
        $postData = $this->requestPost; // equivalent to \Yii::$app->request->post()
        $request = $this->request; // equivalent to \Yii::$app->request
        $response = $this->response; // equivalent to \Yii::$app->response
        $session = $this->session; // equivalent to \Yii::$app->session
        $auth = $this->authManager; // equivalent to \Yii::$app->authManager
        $getData = $this->requestGet; // equivalent to \Yii::$app->request->get()
    }
}

When init ActiveAction will check class exist @app\models\controllername\actioname if exist it auto create an object and set to property $model of action class., (*13)

Example: I have a model class app\models\guest\Login Login is a name of my action. In ActionLogin it auto define and set to property $model, (*14)

<?php

namespace app\controllers\guest;

use vuongminh\ac\ActiveAction;

/**
 *
 * @property \app\models\guest\Login $model
 */
class ActionLogin extends ActiveAction
{
    public function run(){
        if($this->model->load($this->requestPost) && $this->model->login()){
            return $this->controller->redirect(['user/home']);
        }
        return $this->controller-render("login");
    }
}

If you want to config model class, you can config it in controller:, (*15)

<?php
namespace app\controllers;

use vuongminh\ac\ActiveController;

class GuestController extends ActiveController
{

     public function listAction()
     {
         return [
             'login' => [
                 'layout' => 'front-end',
                 'model' => 'app\models\Login'
             ],
              'forgot-password' => [
                  'reponseFormat' => \yii\web\Response::FORMAT_JSON,
                  'model' => [
                      'class' => 'app\models\Login',
                      'propertyName1' => 'value',
                      'propertyName2' => 'value'
                  ],
              ]            
         ];
     }   
}

Scenarios in ActiveRecord

Abstract class vuongminh\ac\ActiveRecord is an extension of yii\db\ActiveRecord it help you solve problem below:, (*16)

Your AR model may have many scenarios and maybe one scenario have many sub-scenario and you have many many rules it so difficult control them. My ActiveRecord pattern may help you., (*17)

I used GII to generate a model AR User and I change an extends class \yii\db\ActiveRecord to an abstract class \vuongminh\ac\ActiveRecord and change method rules() to tableRules(), (*18)

<?php

namespace app\models;

/**
 * This is the model class for table "users".
 *
 * @property string $iId
 * @property string $cEmail
 * @property string $cPassword
 */
class User extends \vuongminh\ac\ActiveRecord  {

        public function tableRules() {
            return [
                [['iId'], 'safe'],
                [['cEmail', 'cPassword'], 'string', 'max' => 256],
             ];
        }
}

and I have scenario forgot password model extends User and method rules of this scenario change to scenarioRules, it will merge with table rules, (*19)

<?php

namespace app\models\guest;
use app\models\User;
/**
 * This is the model class for table "users".
 *
 * @property string $iId
 * @property string $cEmail
 * @property string $cPassword
 */
class ForgotPassword extends User  {

        public $cRePassword, $cReCaptcha, $cVerifyCode;

        public function scenarioRules() {
            return [
                [['cEmail', 'cReCaptcha', 'cVerifyCode', 'cPassword', 'cRePassword'], 'required'],
                 [['cEmail'], 'exist'],
                ['cVerifyCode', 'checkVerifyCode'],
                ['cVertifyCode', 'required'],
                ['cRePassword', 'compare', 'compareAttribute' => 'cPassword']
             ];
        }

        public function scenarios(){
            return [
                'sendVerifyCode' => ['cEmail', 'cReCaptcha'],
                'vertifyCode' => ['cVerifyCode', 'cPassword', 'cRePassword']
            ];            
        }
}

The Versions

22/12 2016

dev-master

9999999-dev

Action controller help you code clean

  Sources   Download

MIT

The Requires

 

by VUONG MINH

extension yii2

12/09 2016

v1-beta

1.0.0.0-beta

Action controller help you code clean

  Sources   Download

MIT

The Requires

 

by VUONG MINH

extension yii2