Last active
January 2, 2016 03:59
-
-
Save rafi/8247923 to your computer and use it in GitHub Desktop.
Custom API for modded Kohana
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| Route::set('api', 'api/<version>/<controller>(.<format>)(/<id>)(/<custom>)', | |
| array( | |
| 'version' => 'v1', | |
| 'id' => '\d+', | |
| 'format' => 'json|xml|csv', | |
| )) | |
| ->filter(function($route, $params, $request) | |
| { | |
| // Empty the action name and append custom verb or _collection | |
| $params['action'] = ''; | |
| isset($params['custom']) AND $params['action'] .= '_'.$params['custom']; | |
| $params['id'] === FALSE AND $params['action'] .= '_collection'; | |
| // Resolve bundle name | |
| $bundles = [ | |
| ... | |
| ]; | |
| foreach ($bundles as $name => $controllers) | |
| { | |
| if (in_array($params['controller'], $controllers)) | |
| { | |
| $params['namespace'] .= "\\$name"; | |
| break; | |
| } | |
| } | |
| return $params; | |
| }) | |
| ->defaults(array( | |
| 'namespace' => 'Vendor', | |
| 'directory' => 'API', | |
| 'action' => 'index', | |
| 'id' => FALSE, | |
| )); | |
| // Base controller | |
| protected $action_map = array | |
| ( | |
| Http_Request::POST => 'post', | |
| Http_Request::GET => 'get', | |
| Http_Request::PUT => 'put', | |
| Http_Request::DELETE => 'delete', | |
| 'PATCH' => 'patch', | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment