php - Laravel 4 RESTful Api with content negotiation -


i'm working in restful api laravel, , want use content negotiation in project, don't know how accomplish that. have controllers separted api version, , want distinguish between api versions , use correct controllerdepending on version.

my api router is:

route::group(array('prefix' => 'api'), function() {     route::resource('users', 'api\v1\userscontroller'); }); 

should create api.type filter use in route group or should in route group clousure or maybe, in each controller?

don't afraid branch out application logic library classes. don't have fit inside of laravel's given folder structure.

in fact, adding in own namespaced group of classes can have big benefit. can see setup on creating own application library here.

once have set up, can create class responsibility decide content type return. might based on accept header, url parameter or whatever define (that's you, api creator).

perhaps class take accept header , normalize "json", "xml" , "html".

the request class has some methods if via accept header.

so, in pseudo code (syntax errors follow!), controller might this:

/* /api/users */ public function index() {     // might return "json" or "xml" or "html"     $contenttype = \my\contenttype\class->getcontenttype();      $users = user::all();      // not shown here logic set `content-type` header in returned response (json vs xml vs html)     // perhaps method go implementation of \my\contenttype\class     return view::make('users.'.$contenttype)->with(array( 'users' => $users ));   } 

that's idea of might do. key point working in library can put business logic started idea of how accomplish adding in business logic application.

hope helps.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -