PHPackages                             webulla/yii2-rest - 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. [API Development](/categories/api)
4. /
5. webulla/yii2-rest

ActiveYii2-extension[API Development](/categories/api)

webulla/yii2-rest
=================

Yii2 component for rest api integration

1301PHP

Since Apr 18Pushed 11y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Webulla Yii2 rest actions
=========================

[](#webulla-yii2-rest-actions)

Component library contains standalone actions for processing rest requests.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
$ composer require "webulla/yii2-rest":"*"

```

Configuration
-------------

[](#configuration)

In my project I use [Backbone](http://backbonejs.org/) to work with models on the client side. Therefore, all *ajax* requests from the model (method *Backbone.sync()*) are divided into four types: *GET*, *POST*, *PUT* and *DELETE*. To process these requests, I realized standalone actions.

I immediately give an example, on the basis of which it will be easier to understand how to work with the component. This model of the controller, which is used for working with posts.

```
namespace app\controllers;

use Yii;
use app\models\Post;
use webulla\rest\actions\RestAction;

/**
 * PostController implements the CRUD actions for Post model.
 */
class PostController extends \app\components\web\Controller {

	/**
	 * @inheritdoc
	 */
	public function actions() {
		return [
			'rest' => [
				'class' => RestAction::className(),
				'modelClass' => Post::className(),
			],
		];
	}
}
```

A detailed description of the settings:

```
...
'rest' => [
    // action class
    'class' => RestAction::className(),

    // model class
    'modelClass' => Post::className(),

    // allowed methods for request
    // default: ['get', 'post', 'put', 'delete']
    'methods' => ['get', 'post', 'put', 'delete'],
],
...
```

Allowed the following types of queries:

- *get* - fetch existing model;
- *post* - create new model;
- *put* - update existing model;
- *delete* - delete existing model.

If you transfer data using one of the following methods: get, put, delete - you must also pass the primary key (id attribute) of the model:

```
yoursite.com/post/rest?id=1

```

You can also use short URL for your rest api. To do this, configure the component *urlManager* in your configuration file:

```
...
'urlManager' => [
    // request query "/controller/action?id=1" instead of "?r=controller/action&id=1"
    'enablePrettyUrl' => true,

    // request query "/controller/action" instead of "index.php/controller/action"
    'showScriptName' => false,

    'rules' => [
        // default rule
        'post/rest' => '/post/rest',

        // allow "/post/rest/1" request instead of "/post/rest?id=1"
        'post/rest/' => '/post/rest',
    ]
],
```

Usage
-----

[](#usage)

A simple example, which receives data through the model rest api:

```
$(function() {
    $.ajax({
        url: '/post/rest?id=23',
        type: 'get',
        dataType: 'json',

        success: function(attributes) {
            console.log(attributes);
        }
    });
});
```

Response:

```
{
	"id":"23",
	"user_id":"1",
	"content":"Text here",
	"created_at":"2015-04-04 14:08:10",
	"updated_at":"2015-04-04 14:08:20"
}
```

Security
--------

[](#security)

Rest api as used in my project through javascript, I added the ability to protect your data: you can explicitly specify which security attributes for each type of request. There are two ways for the configuration of safe attributes:

- When you save any data via rest api (*post*, *put*), the model is created with the scenario *rest-save*.
- When you get some data via rest api (*get*), the model is created with the scenario *rest-fetch*.

This is my Post model which I use in rest request:

```
class Post extends \yii\db\ActiveRecord {
    ...
	/**
	 * @inheritdoc
	 */
	public function rules() {
		return [
			...
			// this validator boosted sets the form while preserving the model through the rest api
			[['user_id'], ForceValueValidator::className(), 'value' => Yii::$app->user->getId(), 'on' => 'rest-save'],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function scenarios() {
		return array_merge(parent::scenarios(), [
		    // these attributes are writable by rest api
			'rest-save' => ['user_id', 'content'],

			// these attributes can be read through the rest api
			'rest-fetch' => ['id', 'user_id', 'content', 'created_at', 'updated_at'],
		]);
	}
    ...
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7753223?v=4)[Pavel Kondratenko](/maintainers/gustarus)[@gustarus](https://github.com/gustarus)

---

Top Contributors

[![gustarus](https://avatars.githubusercontent.com/u/7753223?v=4)](https://github.com/gustarus "gustarus (8 commits)")

### Embed Badge

![Health badge](/badges/webulla-yii2-rest/health.svg)

```
[![Health](https://phpackages.com/badges/webulla-yii2-rest/health.svg)](https://phpackages.com/packages/webulla-yii2-rest)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

265103.1M454](/packages/google-gax)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
