开发者

CakePHP - routing with wildcards (controller not found error)

开发者 https://www.devze.com 2023-03-14 02:11 出处:网络
I\'m not really even sure how exactly to search开发者_JAVA百科 for this but I have a URL site.com/forum/controller/action

I'm not really even sure how exactly to search开发者_JAVA百科 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?


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'].

0

精彩评论

暂无评论...
验证码 换一张
取 消