Router::mapResources PHP Method

mapResources() public static method

### Options: - 'id' - The regular expression fragment to use when matching IDs. By default, matches integer values and UUIDs. - 'prefix' - URL prefix to use for the generated routes. Defaults to '/'.
public static mapResources ( string | array $controller, array $options = [] ) : array
$controller string | array A controller name or array of controller names (i.e. "Posts" or "ListItems")
$options array Options to use when generating REST routes
return array Array of mapped resources
    public static function mapResources($controller, $options = array())
    {
        $hasPrefix = isset($options['prefix']);
        $options += array('connectOptions' => array(), 'prefix' => '/', 'id' => static::ID . '|' . static::UUID);
        $prefix = $options['prefix'];
        $connectOptions = $options['connectOptions'];
        unset($options['connectOptions']);
        if (strpos($prefix, '/') !== 0) {
            $prefix = '/' . $prefix;
        }
        if (substr($prefix, -1) !== '/') {
            $prefix .= '/';
        }
        foreach ((array) $controller as $name) {
            list($plugin, $name) = pluginSplit($name);
            $urlName = Inflector::underscore($name);
            $plugin = Inflector::underscore($plugin);
            if ($plugin && !$hasPrefix) {
                $prefix = '/' . $plugin . '/';
            }
            foreach (static::$_resourceMap as $params) {
                $url = $prefix . $urlName . ($params['id'] ? '/:id' : '');
                Router::connect($url, array('plugin' => $plugin, 'controller' => $urlName, 'action' => $params['action'], '[method]' => $params['method']), array_merge(array('id' => $options['id'], 'pass' => array('id')), $connectOptions));
            }
            static::$_resourceMapped[] = $urlName;
        }
        return static::$_resourceMapped;
    }

Usage Example

Example #1
0
 * different urls to chosen controllers and their actions (functions).
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Config
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('CroogoRouter', 'Croogo.Lib');
Router::connect('/signup', array('controller' => 'user', 'action' => 'register'));
Router::connect('/loggout', array('controller' => 'user', 'action' => 'loggout'));
Router::connect('/admin', array('controller' => 'admin', 'action' => 'index'));
Router::connect('/wxapi/*', array('controller' => 'wxapi', 'action' => 'index'));
Router::connect('/admin/wc/*', array('controller' => 'admin', 'action' => 'wc'));
Router::connect('/version', array('controller' => 'user', 'action' => 'version'));
Router::mapResources('users');
Router::mapResources('education');
Router::mapResources('wx');
CakePlugin::routes();
Router::connect('/', array('controller' => 'user', 'action' => 'login'));
Router::parseExtensions('json', 'rss');
CroogoRouter::localize();
require CAKE . 'Config' . DS . 'routes.php';
All Usage Examples Of Router::mapResources