2012-07-26 48 views
0

我已经为我的应用程序编写了静态页面组件,其中管理员可以动态添加/编辑/删除静态内容页面。这些都保存在数据库中。检查现有的控制器

(例如,你可以创建一个名为“关于”页面,可以在所有MyApplication /参观约)

这是我对这些页面的路由:

$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页面时,它将覆盖RouteControl到UsersController。

所以我需要像黑名单或类似的东西:我开始写一个验证规则,在那里我想检查,如果该控制器存在。对于Cake 1.3,有一个函数“loadController”,如果控制器不存在,则返回false,但对于cake 2.x,不存在这样的函数。我想知道这个吗?它有新的名字吗?或者现在在实用程序库中?

或者有更好的方法来解决这个问题吗?

+0

我不知道,但你可能不得不使用异常处理。 http://book.cakephp.org/2.0/en/development/exceptions.html#exception-classes。 MissingControllerException是一个预定义的异常类,可以在尝试访问不存在的控制器时使用。 – 2012-07-26 10:26:51

+0

CakePHP 2.X引入了一个包含_loadController()方法的新类“CakeRequest”。 http://api20.cakephp.org/class/dispatcher#method-Dispatcher_loadController此链接可能会对您有所帮助。 – 2012-07-26 10:30:39

回答

0

的名字这是我的验证方法现在:

$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; 
    }