PHPackages                             mobileka/scope-applicator-yii2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mobileka/scope-applicator-yii2

ActiveLibrary[Framework](/categories/framework)

mobileka/scope-applicator-yii2
==============================

Scope Applicator bindings for Yii2 Framework

1.0.3(9y ago)19MITPHPPHP &gt;=5.4.0

Since Jul 4Pushed 9y agoCompare

[ Source](https://github.com/mobileka/scope-applicator-yii2)[ Packagist](https://packagist.org/packages/mobileka/scope-applicator-yii2)[ RSS](/packages/mobileka-scope-applicator-yii2/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/6363eced24eb0a34bfe56f74391fa6451fafe7d83de5939727d4d99e31415017/68747470733a2f2f7472617669732d63692e6f72672f6d6f62696c656b612f73636f70652d6170706c696361746f722d796969322e737667)](https://travis-ci.org/mobileka/scope-applicator-yii2)[![Code Climate](https://camo.githubusercontent.com/b74830e93e5c39f7e64cd572f28b73a0214aa442877f3b172e8b74dcc8a7f0c6/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6f62696c656b612f73636f70652d6170706c696361746f722d796969322e737667)](https://codeclimate.com/github/mobileka/scope-applicator-yii2)[![Coverage Status](https://camo.githubusercontent.com/6c25c4f107a1e3ae270cc498c520d5078e90520bb67e32e9f02aea33fa9bec0d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d6f62696c656b612f73636f70652d6170706c696361746f722d796969322f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/mobileka/scope-applicator-yii2?branch=master)

[ScopeApplicator](https://github.com/mobileka/scope-applicator) brings an elegant way of sorting and filtering data to your Yii2 projects.

- [Overview](#overview)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)

Overview
--------

[](#overview)

ScopeApplicator is an easy and logical way to achieve something like this:

`/posts` – returns a list of all posts

`/posts?recent` – returns only recent posts

`/posts?author_id=5` – returns posts belonging to an author with an `id=5`

`/posts?author_id=5&order_by_title=desc&status=active` – returns only active posts belonging to an author with an `id=5` and sorts them by a title in a descending order

Requirements
------------

[](#requirements)

– PHP 5.4 or newer

– Yii 2.0.x

Installation
------------

[](#installation)

`composer require mobileka/scope-applicator-yii2 1.0.*`

Usage
-----

[](#usage)

Let's learn by example. First of all, we'll implement an `author_id` filter for `post` table.

These are steps required to achieve this:

1. Create a `PostQuery` class which extends `Mobileka\ScopeApplicator\Yii2\ActiveQuery` and define a `userId` method
2. Create a `Post` model which extends `Mobileka\ScopeApplicator\Yii2\Model` and make it use the `PostQuery` instead of a regular `ActiveQuery`
3. Create a basic `PostController` which outputs a list of posts when `/posts` route is hit
4. Tell ScopeApplicator that `userId` scope is available and give it an alias
5. Visit `/posts?author_id=1` and enjoy the result

Ok, let's cover these step by step.

— Create a `PostQuery` class in `app/models/queries` directory with the following content:

```
