Hi folks,
I just pushed an alpha version of "ActiveResource for Yii" to Github (https://github.com/Haensel/ActiveResource)
What is Active Resource?
The goal is to use REST services and their resources as if they were schemaless ActiveRecord models. I was using a lot of different REST services these days (Neo4j for example is a graph database exposed as RESTful service) and soon recognized that a lot of them could need the same features an CActiveRecord model would provide.
So instead of using a database as persistent storage this class uses a REST service. This project is inspired by the Rails version of ActiveResource (http://api.rubyonrails.org/classes/ActiveResource/Base.html)
How do you I use it?
Please follow the instructions on GitHub. It should be really straight forward.
Give me an example so I can see if this could be interesting for me:
Imagine a webservice exposing "people" resources via REST. A simple EActiveResource model could look like this:
<?php
class Person extends EActiveResource
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function rest()
{
return array(
'site'=>'http://api.aRESTservice.com',
'resource'=>'people',
'contenttype'=>'application/json',
'accepttype'=>'application/json',
'fileextension'=>'.json', //Twitter always wants you to append this to your GET requests for example
);
}
}
?>
Then you want to add a new Person
$person=new Person;
$person->setAttributes(array(
'name'=>'Haensel',
'gender'=>'m'
));
$person->save(); //validation fails, no POST request is sent to the service. You can get the error messages like you would with CActiveRecord
$person=new Person;
$person->setAttributes(array(
'name'=>'Haensel',
'gender'=>1
));
$person->save(); // VALIDATED. Sending POST request to http://api.aRESTservice.com/people with data '{'name':'Haensel','gender':1}'
Ok, so why isn’t this an extension already?
This is an early version and I don’t want you to be mad at me when it sucks. For example basic authentication isn’t fully implemented yet although that should be easy. OAuth for example could be a bit more tricky. And it isn’t possible to send XML, you are only able to RECEIVE XML and JSON or send JSON or urlencoded data.
If the release becomes stable I’ll sure add it to the extensions.
So what do you think? Nice, bad? Anything is welcome and if you find bugs or general problems you can post it to my GitHub repository. This should be the HQ. If you have any additional question drop me a line on twitter (@Haensel).
I also used extensive tracing, so activate tracing and look for "ext.EActiveResource" to find possible problems.
Cheers,
Haensel