Created
          February 2, 2015 19:30 
        
      - 
      
- 
        Save bravo-kernel/189cc3f340013f8c27be to your computer and use it in GitHub Desktop. 
    mapResources() for ApiListener
  
        
  
    
      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
    
  
  
    
  | /** | |
| * 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