yii2-webivan1/yii2-validate-action-params
Composer 安装命令:
composer require yii2-webivan1/yii2-validate-action-params
包简介
Extension Yii2
README 文档
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
统计信息
- 总下载量: 220
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-29