0

I'm creating a plugin for my application (using CakePHP 2.6.0) that allows users to login into a user area using the same model as for the admin area, so I'm trying to get the same type of URI scheme as the admin area e.g. /admin/users/login but then for /special/users/login. I have the following route in my plugin's Config/routes.php and added the 'special' prefix to the 'Routing.prefixes' configuration:

Router::connect('/special/:controller/:action', array(
    'special' => true,
    'prefix' => 'special',
    'plugin' => 'Special',
    'controller' => ':controller',
    'action' => ':action',
));

With above route and entering the following url /special/users/login it would make sense to me right now if Cake went for Plugin/Controller/SpecialUsersController.php (considering namespace conflicts with the main application's UsersController) but instead I get an error Error: Create the class UsersController.

Is there a built-in way for it to load a prefixed controller (without changing the url) based on the plugin? Or is there a better way to neatly extend my main application? Am I going about this the wrong way?

Max
  • 21
  • 4

1 Answers1

0

I could not find a built-in way for it to work as I wanted to so I changed the way URL strings were parsed using a custom route class in my app's Routing/Route/ folder:

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

/**
 * Plugin route will make sure plugin routes get
 * redirected to a prefixed controller.
 */
class PluginRoute extends CakeRoute {

    /**
     * Parses a string URL into an array.
     *
     * @param string $url The URL to parse
     * @return bool False on failure
     */
    public function parse($url) {
        $params = parent::parse($url);

        if($params && !empty($params['controller']) && !empty($params['plugin'])) {
            $params['controller'] = $params['plugin'] . ucfirst($params['controller']);
        }

        return $params;
    }

}

And then setting up my plugin routes as:

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

Router::connect('/special/:controller/:action', array(
    'special' => true,
    'prefix' => 'special',
    'plugin' => 'Special',
    'controller' => ':controller',
    'action' => ':action',
), array(
    'routeClass' => 'PluginRoute'
));

This resulted in /special/users/login creating the controller I wanted Plugin/Controller/SpecialUsersController.php, no side effects so far. Extending the main application's UsersController is a different story though.

Maybe someone else has use of this or knows a better solution?

Max
  • 21
  • 4