phpcakephpurl-routingcakephp-routing

CakePHP - routing with wildcards (controller not found error)


I'm not really even sure how exactly to search for this but I have a URL

site.com/forum/controller/action

Where forum is a plugin and I currently have it routing to the plugin forum successfully with

Router::connect('/forum', array('plugin' => 'forum', 'controller' => 'home', 'action' => 'index'));

However, I want to add a route that will connect any top-level subdirectory to the plugin forum. For example,

site.com/fish/controller/action
site.com/bird/controller/action

would both route to the forum plugin. Similarly,

site.com/bird

would also route to the forum plugin. This is the best I have been able to come up with and it has no effect (I get a "FishController could not be found":

Router::connect('/*/:controller/:action/*', array('plugin' => 'forum'));

The closest answer I could find basically says this might not be possible? http://cakephp.1045679.n5.nabble.com/Routes-with-wildcards-missing-controller-errors-td1263632.html

EDIT: After some more trial & error I tried this:

Router::connect('/:site/:controller/:action/*', array('plugin' => 'forum'));

And it works. Could someone explain this?


Solution

  • The documentation at http://api.cakephp.org/class/router#method-Routerconnect does a great job at explaining this.

    What you've done is created a custom parameter. Cake uses an array to keep track of the parameters and that how it know which controller, action and other parameters have been passed. The Router will convert any URLs with 3 slashes (/) to $param['site'], $param['controller'] and $param['action'].

    From your controller, you can retrieve the value of :site by using $this->params['site'].