Skip to content

davidevernizzi/yii_restful_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yii_restful_api

A simple REST API written with Yii 1.1.x

Installation

  1. Install a LAMP environment and composer
  2. git clone
  3. cd protected
  4. composer install
  5. run migrations
  6. run tests:
  • cd protected/tests
  • ../vendor/bin/phpunit unit/

Usage

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();
    }
}

TODO

References

About

A REST API with Yii 1.1.x

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages