inabhi9 / activeresource
Yii 框架扩展,用于消费 REST API
Requires
- php: >=5.1.0
This package is not auto-updated.
Last update: 2024-09-28 15:19:04 UTC
README
##EActiveResource for Yii
...是 Yii PHP 框架的一个扩展,允许用户创建使用 RESTful 服务作为持久化存储的模型。实现灵感来源于 Yii 的 CActiveRecord 类(https://yiiframework.cn/doc/api/1.1/CActiveRecord/)和 Ruby on Rails 的 ActiveResource 实现(《https://api.rubyonrails.cn/classes/ActiveResource/Base.html》)。
##提示:警告:这仍然是一个 alpha 版本!该项目作为一个草案开始,仍在开发中,因此在 1.0 版本发布之前,您可能会遇到可能导致代码中断的更改。请参阅 CHANGES.md 文件以获取更多信息
由于有成千上万的 REST 服务使用不同的方法,调试错误可能很复杂。因此,我在所有主要函数中添加了广泛的跟踪,这样您应该总能看到每个请求,它使用的方法以及服务如何响应。只需启用 Yii 的跟踪功能,并查找“ext.EActiveResource”类别
##安装
1.) 将扩展添加到 Yii 中,将其放置在您的应用程序扩展文件夹中(例如 '/protected/extensions') 2.) 编辑应用程序的 main.php 配置文件,将 'application.extensions.EActiveResource.*' 添加到您的导入定义中 3.) 将您的资源配置添加到主配置文件
'activeresource'=>array(
'class'=>'EActiveResourceConnection',
'site'=>'http://api.aRESTservice.com',
'contentType'=>'application/json',
'acceptType'=>'application/json',
)),
'queryCacheId'=>'SomeCacheComponent')
4.) 现在创建一个扩展 EActiveResource 的类,如下所示(不要忘记 model() 函数!)
##快速概述
class Person extends EActiveResource
{
/* The id that uniquely identifies a person. This attribute is not defined as a property
* because we don't want to send it back to the service like a name, surname or gender etc.
*/
public $id;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function rest()
{
return CMap::mergeArray(
parent::rest(),
array(
'resource'=>'people',
)
);
}
/* Let's define some properties and their datatypes
public function properties()
{
return array(
'name'=>array('type'=>'string'),
'surname'=>array('type'=>'string'),
'gender'=>array('type'=>'string'),
'age'=>array('type'=>'integer'),
'married'=>array('type'=>'boolean'),
'salary'=>array('type'=>'double'),
);
}
/* Define rules as usual */
public function rules()
{
return array(
array('name,surname,gender,age,married,salary','safe'),
array('age','numerical','integerOnly'=>true),
array('married','boolean'),
array('salary','numerical')
);
}
/* Add some custom labels for forms etc. */
public function attributeLabels()
{
return array(
'name'=>'First name',
'surname'=>'Last name',
'salary'=>'Your monthly salary',
);
}
}
##使用方法
/* sends GET to http://api.example.com/person/1 and populates a single Person model*/
$person=Person::model()->findById(1);
/* sends GET to http://api.example.com/person and populates Person models with the response */
$persons=Person::model()->findAll();
/* create a resource
$person=new Person;
$person->name='A name';
$person->age=21;
$person->save(); //New resource, send POST request. Returns false if the model doesn't validate
/* Updating a resource (sending a PUT request)
$person=Person::model()->findById(1);
$person->name='Another name';
$person->save(); //Not at new resource, update it. Returns false if the model doesn't validate
//or short version
Person::model()->updateById(1,array('name'=>'Another name'));
/* DELETE a resource
$person=Person::model()->findById(1);
$person->destroy(); //DELETE to http://api.example.com/person/1
//or short version
Person::model()->deleteById(1);