CakePHP Router Configuration Removing Pages in a URL

I'm currently looking for a solution to remove the "Pages" from the URL, but it doesn't make it difficult to add a controller.

I looked around and searched on Google for a solution, but the best solution they could give was the following:

Router::Connect('/*', array('controller' => 'pages', 'action' => 'display'));

Link Example

But the problem with this solution is that every time I want to add a controller, I need to add the following lines to the config / routers.php file

Router::Connect('/Users/:action/, array('controller' => 'Users');
Router::Connect('/users/:action/, array('controller' => 'Users');

The reason I added these two lines is because I did not want the url to be case sensitive.

I also know that it’s not as often as the site is “COMPLETED” that the controllers change.

So my solution was as follows:

/*
 * Get URL
 * Seperate in array
 * Remove empty elements
 */
$url = array_filter(explode("/", Router::url()));

/*
 * Get Controllers list
 */
$Controllers = App::objects('controller');
$CleanControllers = array();

/*
 * Remove string 'Controller' from element
 * lower string
 */
foreach($Controllers as $Controller) {
    $CleanControllers[] = strtolower(str_replace('Controller', '', $Controller));
}

/*
 * Check if first element, from URL, is NOT a controllers array
 */
if(!in_array(strtolower(reset($url)), $CleanControllers)) {
    /*
     * Forward to "Pages" controller 
     */
    Router::connect('/', array('controller' => 'Pages', 'action' => 'display', 'home'));
    Router::connect('/*', array('controller' => 'Pages', 'action' => 'display'));
}

/*
 * Check if first element, from URL, is NOT a controllers array
 */

CakePlugin::routes();

require CAKE . 'Config' . DS . 'routes.php';

So my question is a KILLER performance solution? If so, does anyone know a better solution?

, CakePHP, , SUPER Advanced routing... .

CAKEPHP 2.1

!

+3
1

, , . , , - ().

0

All Articles