您可以routeClass
在指定路由时使用该属性,并使用自定义路由类。
这是我的CakeRoute
类的实现,它完全符合您的描述(将控制器前缀附加到您的控制器):
// ControllerPrefixRoute.php file in app/Routing/Route/
App::uses('CakeRoute', 'Routing/Route');
class ControllerPrefixRoute extends CakeRoute {
/**
* Parses a string url into an array. If a controller_prefix key is found it will be appended to the
* controller parameter
*
* @param string $url The url to parse
* @return mixed false on failure, or an array of request parameters
*/
public function parse($url) {
$params = parent::parse($url);
if (!$params) {
return false;
}
$params['controller'] = $params['controller_prefix'].'_'.$params['controller'];
return $params;
}
}
以下是如何使用它:
// inside routes.php file in app/Config/
App::uses('ControllerPrefixRoute', 'Routing/Route');
Router::connect("/:controller_prefix/:controller/:action/*", array(), array('routeClass' => 'ControllerPrefixRoute'));
所以这个 url/backend/settings/myaction
将调用BackendSettingsController::myaction