Cakephp 检查现有控制器
我已经为我的应用程序编写了静态页面组件,管理员可以在其中动态添加/编辑/删除静态内容页面。这些都保存在数据库中 (例如,您可以创建一个名为“关于”的页面,并可以通过myapplication/about访问该页面) 这是我对这些页面的路由:Cakephp 检查现有控制器,cakephp,routing,Cakephp,Routing,我已经为我的应用程序编写了静态页面组件,管理员可以在其中动态添加/编辑/删除静态内容页面。这些都保存在数据库中 (例如,您可以创建一个名为“关于”的页面,并可以通过myapplication/about访问该页面) 这是我对这些页面的路由: $page = new StaticPage(); $slugs = $page->find('list', array( 'fields' => array('slug'), 'recursive' => -1,
$page = new StaticPage();
$slugs = $page->find('list', array(
'fields' => array('slug'),
'recursive' => -1,
'order' => 'StaticPage.slug DESC',
));
Router::connect('/:slug',
array('controller' => 'static_pages', 'action' => 'display'),
array(
'pass' => array('slug'),
'slug' => implode($slugs, '|')
)
);
现在我有一个问题,当你创建一个slug与现有控制器(例如用户)匹配的页面时,它会覆盖到UsersController的路由
所以我需要一个类似于黑名单或类似的东西:我开始编写一个验证规则,在这里我想检查控制器是否存在。对于cake 1.3,有一个函数“loadController”,如果控制器不存在,则返回false,但是对于cake 2.x,没有这样的函数。不知为什么我错过了这个?它是否有新名称或现在在实用程序库中
或者有更好的方法解决这个问题吗?您应该尝试以下方法:
通过获取所有控制器的列表,您可以轻松排除控制器的名称,这是我目前的验证方法:
$route = Router::parse($check['slug']);
$controllerName = Inflector::camelize($route['controller'] . 'Controller');
$aCtrlClasses = App::objects('controller');
foreach ($aCtrlClasses as $controller) {
if ($controller != 'AppController') {
// Load the controller
App::import('Controller', str_replace('Controller', '', $controller));
// Load the ApplicationController (if there is one)
App::import('Controller', 'AppController');
$controllers[] = $controller;
}
}
if (in_array($controllerName, $controllers)) {
return false;
} else {
return true;
}
我不确定,但您可能必须使用异常处理。MissingControllerException是一个预定义的异常类,您可以在尝试访问不存在的控制器时使用它。CakePHP 2.X引入了一个新类“CakereRequest”,其中包含_loadController()方法。此链接可能对您有所帮助。