Yii Framework Forum: Snippet: Restful Curlmanager Rules For Elements & Collections - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Snippet: Restful Curlmanager Rules For Elements & Collections Easily configured for your needs Rate Topic: -----

#1 User is offline   danschmidt5189 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 19-February 13

Posted 30 May 2013 - 03:15 PM

These rules are helpful for anyone looking for a simple, pre-configured RESTful routing scheme. (This would go in your UrlManager::$rules configuration.) Save yourself some time and pick these up, or reply with suggestions for improvements.

See the code comment below for details.

Edit: Changed to a nested-module based format to improve versioning, plus extracts the
<?php
/**
 * API Routing Rules
 * 
 * Supports three endpoint styles:
 * - Collection:         /foos
 * - Item:               /foos/{fooId}
 * - Related Collection: /food/{fooId}/bars
 * 
 * Detect file extension:
 * - /foos.json => ?returnType=json
 * - /bars.xml  => ?returnType=xml
 * 
 * Require version #:
 * - /api/v1/foobars.xml => ?apiVersion=v1&returnType=xml
 * 
 * For versioning you're probably best off creating an ApiModule, with sub-modules for each version, e.g. api.modules.V1Module.
 **/
$fileExt = 'json|xml';
return array(
	array(
		'api/<apiVersion>/<controller>/viewResource',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>/<id:\d+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'GET',
	),
	array(
		'api/<apiVersion>/<controller>/viewRelatedCollection',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>/<id:\d+>/<relationName:\w+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'GET',
	),
	array(
		'api/<apiVersion>/<controller>/deleteResource',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>/<id:\d+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'DELETE',
	),
	array(
		'api/<apiVersion>/<controller>/updateResource',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>/<id:\d+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'PUT,POST',
	),
	array(
		'api/<apiVersion>/<controller>/viewCollection',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'GET',
	),
	array(
		'api/<apiVersion>/<controller>/createResource',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'POST',
	),
	array(
		'api/<apiVersion>/<controller>/updateCollection',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'PUT',
	),
	array(
		'api/<apiVersion>/<controller>/deleteCollection',
		'pattern' =>'api/<apiVersion:v\d+>/<controller:\w+>\.?<returnType:('.$fileExt.')?>',
		'verb'    =>'DELETE',
	),
);

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users