yii2-webivan1/yii2-validate-action-params

Extension Yii2

Installs: 89

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 1

Open Issues: 1

Type:yii2-extension

v0.0.2 2018-11-05 17:55 UTC

README

Ext Yii 2 Validate action params, DI action

Install

composer require yii2-webivan1/yii2-validate-action-params

Settings

Add trait in any controller or global controller for trigger event before runAction

    use webivan\validateAction\ValidateActionTrait;

Add behavior in controller

    public function behaviors()
    {
        return [
            'validation' => [
                'class' => webivan\validateAction\ValidateActionBehavior::class,
                // 'actions' => ['index', 'about'] // Validate only action
            ]
        ];
    }

Usage

Add types by params action [php7]

    public function actionIndex(int $number, string $name, array $data) 
    {
        // ...
    }

Or add phpdoc by action together with tag @validate

    /**
     * @validate
     * @param integer $number
     * @param string $name
     * @param array $data
     */
    public function actionIndex($number, $name, array $data) 
    {
        // ...
    }

Dependency injection

You can add DI in action

    public function actionIndex(Request $request, int $number, string $name, array $data, Response $response) 
    {
        // ...
    }

Or phpdoc. Write the full path to the class name!

    /**
     * @validate
     * @param \yii\web\Request $request
     * @param integer $number
     * @param string $name
     * @param array $data
     * @param Response $response
     */
    public function actionIndex($request, $number, $name, $data, Response $response) 
    {
        // ...
    }

Add models [ActiveRecord]

    // Usually
    public function actionIndex($cityId) 
    {
        if (is_null($city = City::findOne(['id' => intval($cityId)]))) {
            throw new HttpExceprion(404);
        }
        
        return $city;
    }
    
    // Now
    public function actionIndex(City $city) 
    {
        return $city;
    }
    
    // Or
    
    /**
     * @validate
     * @param \app\models\City $city
     */
    public function actionIndexNew($city) 
    {
        return $city;
    }
    
    // Or
    
    /**
     * @validate
     * @param City $city
     */
    public function actionIndexNew(City $city) 
    {
        return $city;
    }

User model

    public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            throw new HttpException(404);
        }
        
        return true;
    }

    // If user is guest, Error 404
    public function actionIndexNew(User $user) 
    {
        // Error 404
    }
    
    // If user is login, User::findOne(['id' => \Yii::$app->user->id])
    public function actionIndexNew(User $user) 
    {
        return $user;
    }

How to change the default query DI?

Add interface webivan\validateAction\models\IFindItem in model

How to change the default column search model?

Add interface webivan\validateAction\models\IFindColumn in model

License

MIT