yii-rest-api

这是Yii Framework(http://www.yiiframework.com/)的扩展,可以轻松地将RESTful API添加到现有的Web应用程序中。【 This is extension for Yii Framework (http://www.yiiframework.com/), which can easy add RESTful API to existing web application. 】

  • Owner: ivolovikov/yii-rest-api
  • Platform: BSD, Linux, Mac, Windows
  • License::
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Yii RESTful API

This is extension for Yii Framework (http://www.yiiframework.com/), which can easy add RESTful API to existing web application.

EXAMPLE

Request

 curl http://test.local/api/users \
   -u demo:demo \
   -d email="user@test.local" \
   -d password="passwd"

Response

{
    "object":"rest_user",
    "id":"TEST_ID",
    "email":"user@test.local",
    "name":"Test REST User"
}

INSTALLATION

All of this code yo can find in demo folder.

  • Unpack library folder to %YOUR_EXTENSION_PATH%/yii-rest-api
  • Update yours config/main.php

Add new path of alias at the beginning

YiiBase::setPathOfAlias('rest', '%YOUR_EXTENSION_PATH%/yii-rest-api/library/rest');

Add extension service to preload and components sections

'preload' => array('restService'),  

'components' => array(  
    'restService' => array(  
        'class'  => '\rest\Service',  
        'enable' => strpos($_SERVER['REQUEST_URI'], '/api/') !== false, // for example
    ),  
),

Change routing settings

'urlManager'=>array(
    'urlFormat'      => 'path',
    'showScriptName' => false,
    'baseUrl'        => '',
    'rules' => array(
        array('%YOUR_CONTROLLER%/index',  'pattern' => 'api/%YOUR_CONTROLLER%', 'verb' => 'GET', 'parsingOnly' => true),
        array('%YOUR_CONTROLLER%/create', 'pattern' => 'api/%YOUR_CONTROLLER%', 'verb' => 'POST', 'parsingOnly' => true),
        array('%YOUR_CONTROLLER%/view',   'pattern' => 'api/%YOUR_CONTROLLER%/<id>', 'verb' => 'GET', 'parsingOnly' => true),
        array('%YOUR_CONTROLLER%/update', 'pattern' => 'api/%YOUR_CONTROLLER%/<id>', 'verb' => 'PUT', 'parsingOnly' => true),
        array('%YOUR_CONTROLLER%/delete', 'pattern' => 'api/%YOUR_CONTROLLER%/<id>', 'verb' => 'DELETE', 'parsingOnly' => true),
    )
),
  • Update parent or specific Controller

Add behavior

public function behaviors()
{
    return array(
        'restAPI' => array('class' => '\rest\controller\Behavior')
    );
}

Overwrite render method (if need it)

public function render($view, $data = null, $return = false, array $fields = array())
{
    if (($behavior = $this->asa('restAPI')) && $behavior->getEnabled()) {
        return $this->renderRest($view, $data, $return, $fields);
    } else {
        return parent::render($view, $data, $return);
    }
}

Overwrite redirect method (if need it)

public function redirect($url, $terminate = true, $statusCode = 302)
{
    if (($behavior = $this->asa('restAPI')) && $behavior->getEnabled()) {
        $this->redirectRest($url, $terminate, $statusCode);
    } else {
        parent::redirect($url, $terminate, $statusCode);
    }
}
  • Upate parent or specific ActiveRecord Model (or any other instance of CModel), if you need render rules.

Add behavior

public function behaviors()
{
    return array(
        'renderModel' => array('class' => '\rest\model\Behavior')
    );
}

Add rule

public function rules()
{
    return array(            
        array('field1,field2,field3', 'safe', 'on' => 'render'),
    );
}

METHODS

Methods that can be used after behaviors attached

Controller methods

/**
 * @method bool isPost()
 * @method bool isPut()
 * @method bool isDelete()
 * @method string renderRest(string $view, array $data = null, bool $return = false, array $fields = array())
 * @method void redirectRest(string $url, bool $terminate = true, int $statusCode = 302)
 * @method bool isRestService()
 * @method \rest\Service getRestService()
 */

Model methods

/**
 * @method array getRenderAttributes(bool $recursive = true)
 * @method string getObjectId()
 */

REQUIREMENTS

PHP >= 5.3.0
Yii Framework >= 1.1.8

LICENSE

Copyright (c) 2012 Pays I/O Ltd. (http://pays.io)

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

Main metrics

Overview
Name With Ownerivolovikov/yii-rest-api
Primary LanguagePHP
Program languagePHP (Language Count: 1)
PlatformBSD, Linux, Mac, Windows
License:
所有者活动
Created At2012-10-15 11:47:57
Pushed At2013-07-11 10:10:30
Last Commit At2013-07-11 14:09:53
Release Count0
用户参与
Stargazers Count100
Watchers Count16
Fork Count30
Commits Count26
Has Issues Enabled
Issues Count1
Issue Open Count1
Pull Requests Count0
Pull Requests Open Count0
Pull Requests Close Count0
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private