Skip to content

Instantly share code, notes, and snippets.

@bravo-kernel
Created February 2, 2015 19:30
Show Gist options
  • Save bravo-kernel/189cc3f340013f8c27be to your computer and use it in GitHub Desktop.
Save bravo-kernel/189cc3f340013f8c27be to your computer and use it in GitHub Desktop.
mapResources() for ApiListener
/**
* Automatically create REST resource routes for all controllers found in your main
* application or in a specific plugin to provide access to your resources
* using /controller/id.json instead of the default /controller/view/id.json.
*
* If called with no arguments, all controllers in the main application will be mapped.
* If called with a valid plugin name all controllers in that plugin will be mapped.
* If combined both controllers from the application and the plugin(s) will be mapped.
*
* This function needs to be called from your application's app/Config/routes.php:
*
* ```
* App::uses('ApiListener', 'Crud.Controller/Crud/Listener');
*
* ApiListener::mapResources();
* ApiListener::mapResources('DebugKit');
* Router::setExtensions(array('json', 'xml'));
* Router::parseExtensions();
* ```
*
* @static
* @param string $plugin
* @return void
*/
public static function mapResources($plugin = null) {
$key = 'Controller';
if ($plugin) {
$key = $plugin . '.Controller';
}
$controllers = array();
foreach (App::objects($key) as $controller) {
if ($controller !== $plugin . 'AppController') {
if ($plugin) {
$controller = $plugin . '.' . $controller;
}
array_push($controllers, str_replace('Controller', '', $controller));
}
}
Router::mapResources($controllers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment