yii-restful

Yii RESTful URL Manager
15 followers

Yii RESTful URL Manager

Requirement

  • PHP >= 5.3.0

Installation

Add depend to your project composer.json file

{
    "require": {
        "yii/yiisoft": "dev-master",
        "likai/yii-restful": "dev-master"
    }
}

After, run composer update update composer packages

Usage

1. Add topics resource

return array(
    'urlManager'=>array(
        'class' => 'Likai\\YiiRestful\\UrlManager',
        'urlFormat' => 'path',
        'showScriptName' => false,
        'resources' => arary(
            'topics',
        ),
    ),
);

UrlManager will generate 5 url rules and bind to the controller, like the following

`GET` http://yourdomain/topics          => TopicsController::actionIndex
`GET` http://yourdomain/topics/{id}     => TopicsController::actionShow
`POST` http://yourdomain/topics         => TopicsController::actionCreate
`PUT` http://yourdomain/topics/{id}     => TopicsController::actionUpdate
`DELETE` http://yourdomain/topics/{id}  => TopicsController::actionDelete

2. Specified parameters

'resources' => arary(
    'topics',
    array('posts', 'only' => array('index', 'create')), // just bind Posts::index, Posts::create action
    array('users', 'except' => array('delete', 'update'), // except Posts::delete, Posts::update action
),

ChangeLog

Version v1.0.1 and Version v1.0.2

  • Added composer supported
  • Added namespace supported

Version v1.0.0

  • First release

Github Repository

Total 2 comments

#14586 report it
robregonm at 2013/08/25 01:33pm
Great extension

Cool extension, mainly when you want to add RESTful methods to all the existing controllers.

#14557 report it
androok at 2013/08/22 10:48am
Great addition - unnecessary for one-off usage though

This is a great extension - thanks for adding it.

If someone is just looking for a one-off RESTful controller (such as, if you made a single controller that acts as a web service but the rest of your app is not) - you may just want to look at what URL CUrlManager offers: Since Verb adds the functionality of HTTP Verbs to URL route patterns - you can easily specify which actions in a controller are executed based on route pattern (and suffix if you choose) and HTTP verb used.

For example:

'<controller:\w+>/<action:\w+>\.json' => '<controller>/<action>Json'

will route any /controller/action.json => SomeController::actionJson - but what if you wanted to route a HTTP POST to /controller/ to SomeController::actionCreateJson? You could use the following:

'<controller:\w+>(\/?)$' => array('<controller>/actionCreateJson','caseSensitive'=>false,'verb'=>'POST')

If you'll be incorporating all HTTP verbs in this manner for every controller - it might make more sense to use this extension, but if you'll only be incorporating a few verbs for a few 'web service' controllers, it might make more sense just to use what Yii already has built-in.

Leave a comment

Please to leave your comment.

Create extension