Skip to content

Instantly share code, notes, and snippets.

@bravo-kernel
Created February 3, 2015 13:29
Show Gist options
  • Save bravo-kernel/926f81e1705d78e9c3ca to your computer and use it in GitHub Desktop.
Save bravo-kernel/926f81e1705d78e9c3ca to your computer and use it in GitHub Desktop.
Custom REST routes for api-prefixed route (does not respect Accept Header)
/**
* Prefixed API routes (served using Crud.ApiListener).
*/
Router::prefix('api', function ($routes) {
// Enable .json extension parsing
$routes->extensions(['json', 'xml']);
/**
* API index route.
*/
$routes->connect(
'/:controller',
[
'action' => 'index',
'_method' => 'GET',
'_ext' => ['json', 'xml'],
]
);
/**
* API view route.
*/
$routes->connect(
'/:controller/:id',
[
'action' => 'view',
'_method' => 'GET',
'_ext' => ['json', 'xml'],
],
[
'id' => self::ID . '|' . self::UUID,
'pass' => [
'id',
]
]
);
/**
* API add route.
*/
$routes->connect(
'/:controller',
[
'action' => 'add',
'_method' => 'POST',
'_ext' => ['json', 'xml'],
]
);
/**
* API edit route.
*/
$routes->connect(
'/:controller/:id',
[
'action' => 'edit',
'_method' => 'PUT',
'_ext' => ['json', 'xml'],
],
[
//'id' => '@todo: someUuidRegEx for more strict checking',
'id' => self::ID . '|' . self::UUID,
'pass' => [
'id',
]
]
);
/**
* API delete route.
*/
$routes->connect(
'/:controller/:id',
[
'action' => 'delete',
'_method' => 'DELETE',
'_ext' => ['json', 'xml'],
],
[
//'id' => '@todo: someUuidRegEx for more strict checking',
'id' => self::ID . '|' . self::UUID,
'pass' => [
'id',
]
]
);
// No fallback routes for the API, all non-matches should 404
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment