开发者

CakePHP routing syntax

开发者 https://www.devze.com 2023-03-01 14:57 出处:网络
How do I do a simple route in CakePHP? I need that each and every URL will be routed by swapping the action and the controller.

How do I do a simple route in CakePHP?

I need that each and every URL will be routed by swapping the action and the controller. I just couldn't understand the placeholders syntax.

Example:

/files/read/3

to

/read/files/3

-- supplemental --

In my application I use aliases for the controllers. and I want to route every url that have a certain keyword, as an action, to a certain controller.

I also want to provide the original controller name as a parameter.

Here is a 1:1 example: There are to alises: fruits and streets.

The keyword that I want to catch in the action is find.

The new controller name is finder.

The following calls match my condition:

/fruits/find/apple/red and /streets/find/longer

The router should catch these urls and convert them, to:

/finder/fruits/apple/red(or supply the parameters in other way, I don't mind) and /finder/streets/longer

How shou开发者_如何学JAVAld it be done?


Here is the line of code that you need to put in /app/config/routes.php:

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

Know more: As you can see from the CakePHP book, there are some 'reserved' patterns for routing configuration. An example would be what I used in the line above: :action and :controller. These patterns allow you to tweak routes extensively.

Beware: changing the order of controller and actions in urls might have unintended consequences in the functionality of other CakePHP features. I haven't tested thoroughly, but this is just a general warning.

Beware: Also, I noticed that you put in your example: /files/read/3. Maybe this was just some dummy example, but if you indeed plan to have an MVC named as 'file', be advised that it will conflict will CakePHP core classes (e.g. File model will conflict with File class).

Anyway, hope this answer helps you well. And I really like how the change of controller and action names make the url more readable. :D

0

精彩评论

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

关注公众号