A simple REST API written with Yii 1.1.x
- Install a LAMP environment and composer
git clone
cd protected
composer install
- run migrations
- run tests:
cd protected/tests
../vendor/bin/phpunit unit/
Create a new controller for the resource you need:
<?php
class FooController extends ApiController
{
public function actionIndex()
{
/* Implementation for GET */
}
public function actionCreate()
{
/* Implementation for POST */
}
public function actionUpdate()
{
/* Implementation for PUT */
}
public function actionDelete()
{
/* Implementation for DELETE */
}
}
If any verb is not used, simply do not write its function. The following example shows a resource that only responds to GET. For other verbs a 501: not implemented
will be returned
<?php
class BarController extends ApiController
{
public function actionIndex()
{
/* Implementation for GET */
}
}
That's all.
You can also create mockups. The following API returns data from the file protected/mockups/foobar_index
(in general, filename convention is controller_action). If the file is not found, it returns a 501: not implemented
.
<?php
class FoobarController extends ApiController
{
public function actionIndex()
{
$this->mock();
}
}
- create table for api tokens
- API Authentication
- Get token out of band
- Get token through username/password (see http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified @ Authorization/Other/Password)
- Client signup/login
- Google+
- FB ?
- Twitter ?
- Yii usergroups ?
- Log
- All calls with params + response
- Last usage
- API Token namagement
- CRUD
- Expiration
- Consider using HAL http://stateless.co/hal_specification.html
- Quick description of OAuth: http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified
- RESTFul Cookbook: http://restcookbook.com/
- How to compute API call digest: http://broadcast.oreilly.com/2009/12/principles-for-standardized-rest-authentication.html @ Query Authentication