matthew-p / yii2-extended-api
Extension extends the capabilities of standard classes Yii2 API
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 726
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.4.0
- matthew-p/yii2-services: ^3.0
- yiisoft/yii2: ^2.0.15
Requires (Dev)
README
Package extends the capabilities of standard classes Yii2 API
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist matthew-p/yii2-extended-api "*"
or add
"matthew-p/yii2-extended-api": "@dev"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by:
REST Controller for ExampleProduct model:
use MP\ExtendedApi\EActiveController; class ProductsController extends EActiveController { /** * @var string */ public $modelClass = ExampleProduct::class; /** * @var string */ public $searchClass = ExampleProductSearch::class; /** * @var bool */ public $errorFilter = true; /** * @var array */ public $externalActions = [ 'delete-all' => true, ]; /** * @inheritdoc */ public function behaviors(): array { return array_merge(parent::behaviors(), [ 'authenticator' => [ 'class' => HttpBearerAuth::class, ], 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => [User::ROLE_API], ], ], ], ]); } /** * @inheritdoc * @throws NotFoundHttpException */ public function filterError() { throw new NotFoundHttpException(Yii::t('app', 'Product not found'), self::FILTER_ERROR_CODE); } }
REST Search model for ExampleProduct:
use MP\ExtendedApi\ModelSearchInterface; class ExampleProductSearch extends ExampleProduct implements ModelSearchInterface { /** * @inheritdoc */ public function rules(): array { // model rules ... } /** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search(array $params = []): ActiveDataProvider { $dataProvider = $this->getDataProvider(); $query = $dataProvider->query; $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions return $dataProvider; } /** * @inheritdoc */ public function getDataProvider(): ActiveDataProvider { return new ActiveDataProvider([ 'query' => self::find(), 'pagination' => [ 'defaultPageSize' => 20, 'pageSizeLimit' => [ 0, 20, 50, 100, ], ], ]); } }
Features:
- Delete all models action (support filtering, add headers)
- Update all models action (support filtering, add headers)
- Filtering via custom data provider
- Custom error if filter result empty
- View action trigger
// In controller $this->action->on(EViewAction::EVENT_RUN_VIEW_ACTION, function (Event $event) use ($action) { // you code... });
- Edit filter params
// public function beforeAction($action) in controller if ($action instanceof EIndexAction) { $filterParams = $action->getFilterParams(); $filterParams['active'] = ExampleProduct::STATUS_ACTIVE; $action->setFilterParams($filterParams); }
- After prepare data provider trigger
- Get deleted model for
delete
action
// public function afterAction($action, $result) if ($action instanceof EDeleteAction) { $model = $action->getModel(); }
- Custom query condition index and delete-all actions
use MP\ExtendedApi\EActiveController;
class ProductsController extends EActiveController
{
public $actionsParams = [
'index' => [
'addQuery' => [self::class, 'customCondition'],
],
];
public static function customCondition($query)
{
...
}
...
- Filter by user for index and delete-all actions
use MP\ExtendedApi\EActiveController;
class ProductsController extends EActiveController
{
public $actionsParams = [
'index' => [
'filterUser' => 'user_id', // table column name
],
];
...
That's all. Check it.