Skip to content

Instantly share code, notes, and snippets.

@rafi
Last active January 2, 2016 03:59
Show Gist options
  • Select an option

  • Save rafi/8247923 to your computer and use it in GitHub Desktop.

Select an option

Save rafi/8247923 to your computer and use it in GitHub Desktop.
Custom API for modded Kohana
<?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