rookian
rookian

Reputation: 1064

Check for existing controller

I've written static pages component for my application, where admins can dynamically add/edit/remove static content pages. these are saved in the database.

(e.g. you can create a page called "about" and can visit it at myapplication/about)

This is my routing for these pages:

$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, '|')
    )
);

Now i have the problem, that when you create a page which slug matches an existing controller (e.g. users), it overwrites the Route to the UsersController.

so i need something like a blacklist or similar: i began to write a validation rule, where i want to check if that controller exists. for cake 1.3 there was a function "loadController" which return false, if the controller did not exist, but for cake 2.x there is no such an function. am i missing this somehow? does it have a new name or is in a utility library now?

Or are there better ways to solve this?

Upvotes: 0

Views: 1282

Answers (2)

rookian
rookian

Reputation: 1064

This is my validation method for now:

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

Upvotes: 0

Krishna
Krishna

Reputation: 1540

you should try this : http://www.cleverweb.nl/cakephp/list-all-controllers-in-cakephp-2/

and by getting the list of all controllers you can easily exclude the name of controllers

Upvotes: 2

Related Questions