/** * sets the crud mappings for prefix routes. * * @return void */ protected function _setPrefixMappings() { $crud = array('create', 'read', 'update', 'delete'); $map = array_combine($crud, $crud); $prefixes = Router::prefixes(); if (!empty($prefixes)) { foreach ($prefixes as $prefix) { $map = array_merge($map, array($prefix . '_index' => 'read', $prefix . '_add' => 'create', $prefix . '_edit' => 'update', $prefix . '_view' => 'read', $prefix . '_remove' => 'delete', $prefix . '_create' => 'create', $prefix . '_read' => 'read', $prefix . '_update' => 'update', $prefix . '_delete' => 'delete')); } } $this->mapActions($map); }
/** * testParsingWithLiteralPrefixes method * * @return void */ public function testParsingWithLiteralPrefixes() { Configure::write('Routing.prefixes', []); Router::reload(); $adminParams = array('prefix' => 'admin'); Router::connect('/admin/:controller', $adminParams); Router::connect('/admin/:controller/:action', $adminParams); Router::connect('/admin/:controller/:action/*', $adminParams); $request = new Request(); Router::setRequestInfo($request->addParams(array('plugin' => null, 'controller' => 'controller', 'action' => 'index'))->addPaths(array('base' => '/base', 'here' => '/', 'webroot' => '/base/'))); $result = Router::parse('/admin/posts/'); $expected = array('pass' => [], 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'index'); $this->assertEquals($expected, $result); $result = Router::parse('/admin/posts'); $this->assertEquals($expected, $result); $result = Router::url(array('prefix' => 'admin', 'controller' => 'posts')); $expected = '/base/admin/posts'; $this->assertEquals($expected, $result); $result = Router::prefixes(); $expected = []; $this->assertEquals($expected, $result); Router::reload(); $prefixParams = array('prefix' => 'members'); Router::connect('/members/:controller', $prefixParams); Router::connect('/members/:controller/:action', $prefixParams); Router::connect('/members/:controller/:action/*', $prefixParams); $request = new Request(); Router::setRequestInfo($request->addParams(array('plugin' => null, 'controller' => 'controller', 'action' => 'index'))->addPaths(array('base' => '/base', 'here' => '/', 'webroot' => '/'))); $result = Router::parse('/members/posts/index'); $expected = array('pass' => [], 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'index'); $this->assertEquals($expected, $result); $result = Router::url(array('prefix' => 'members', 'controller' => 'users', 'action' => 'add')); $expected = '/base/members/users/add'; $this->assertEquals($expected, $result); }
* - `/:prefix/:controller/:action/*` * * If plugins are found in your application the following routes are created: * * - `/:plugin` a plugin shortcut route. * - `/:plugin/:controller` * - `/:plugin/:controller/:action/*` * * And lastly the following catch-all routes are connected. * * - `/:controller' * - `/:controller/:action/*' * * You can disable the connection of default routes by deleting the require inside APP/Config/routes.php. */ $prefixes = Router::prefixes(); $prefixPattern = implode('|', $prefixes); $plugins = Plugin::loaded(); foreach ($plugins as $key => $value) { $plugins[$key] = Inflector::underscore($value); } $pluginPattern = implode('|', $plugins); $indexParams = ['action' => 'index']; $pluginShortMatch = ['routeClass' => 'Cake\\Routing\\Route\\PluginShortRoute', '_name' => '_plugin._controller:index']; if ($prefixPattern && $pluginPattern) { $match = ['prefix' => $prefixPattern, 'plugin' => $pluginPattern]; Router::connect('/:prefix/:plugin', $indexParams, $match + $pluginShortMatch); Router::connect('/:prefix/:plugin/:controller', $indexParams, $match); Router::connect('/:prefix/:plugin/:controller/:action/*', [], $match); } if ($pluginPattern) {
/** * Check if the request's action is marked as private, with an underscore, * or if the request is attempting to directly accessing a prefixed action. * * @param \ReflectionMethod $method The method to be invoked. * @param \Cake\Network\Request $request The request to check. * @return bool */ protected function _isPrivateAction(\ReflectionMethod $method, Request $request) { $privateAction = $method->name[0] === '_' || !$method->isPublic() || !in_array($method->name, $this->methods); $prefixes = Router::prefixes(); if (!$privateAction && !empty($prefixes)) { if (empty($request->params['prefix']) && strpos($request->params['action'], '_') > 0) { list($prefix) = explode('_', $request->params['action']); $privateAction = in_array($prefix, $prefixes); } } return $privateAction; }