3

I created a route which is analog to this one:

Router::connect("/backend/:controller/:action/*");

And now I want to route every controller that fits this pattern to be renamed to something like backend_:controller.

Somehow like that:

Router::connect("/backend/:controller/:action/*", array('controller' => 'backend_:controller'));

Example: If the URL www.example.com/backend/settings/myaction is called, it would route to the controller "backend_settings" and invoke the action "myaction"!

But on the other hand, if some called www.example.com/settings, it would route to the controller "settings".

The URL is supposed to stay the way it was called, cakePHP should only use a modified controller name!

I hope someone can point me to the best solution I am supposed to use for this problem. Thanks in advance!

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Dennis
  • 1,805
  • 14
  • 17

2 Answers2

5

You can use the routeClass property when specifying your route, and use a custom route class.

This is my implementation of the CakeRoute class that does exactly what you described (appends a controller prefix to your controller):

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

}

And here is how to use it:

// inside routes.php file in app/Config/

App::uses('ControllerPrefixRoute', 'Routing/Route');

Router::connect("/:controller_prefix/:controller/:action/*", array(), array('routeClass' => 'ControllerPrefixRoute'));

So this url /backend/settings/myaction will invoke BackendSettingsController::myaction

Calin
  • 2,110
  • 1
  • 21
  • 36
1

maybe what you need is a router prefix.

go to the core.php and add this line:

Configure::write('Routing.prefixes', array('backend'));

and that's all... you don't need to add routes.. so now www.example.com/backend/settings/add will look for a method called backend_add() in the Settings controller

And www.example.com/settings/add will call the method called add() in the Settings controller

here you'll find better examples =)

hope this helps

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
  • Thank you for your answer. I already know prefixing. Unfortunately that's not what I want since I want to seperate the backend controllers from the frontend controllers. Using prefixes I would mix backend and frontend methods. The problem here is that my backend "main" controller supports authentification and all controllers which deal with backend are inheriting from this main controller and especially the authentification. Frontend controllers should not deal with authentification at all. Thats why I want to seperate them! – Dennis Dec 23 '11 at 19:19
  • the it seems to me that you'll need to write all the rules for the controllers... o maybe you could use your own class for routing the backend urls http://book.cakephp.org/2.0/en/development/routing.html?highlight=routing#custom-route-classes – pleasedontbelong Dec 24 '11 at 20:08