webulla/yii2-rest
Composer 安装命令:
composer require webulla/yii2-rest
包简介
Yii2 component for rest api integration
关键字:
README 文档
README
Component library contains standalone actions for processing rest requests.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require "webulla/yii2-rest":"*"
Configuration
In my project I use Backbone 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/<id:\d+>' => '/post/rest', ] ],
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
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'], ]); } ... }
webulla/yii2-rest 适用场景与选型建议
webulla/yii2-rest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「extension」 「yii」 「yii2」 「webulla」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webulla/yii2-rest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webulla/yii2-rest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webulla/yii2-rest 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL
- 更新时间: 2015-04-04