Now i need to give this page a url like www.sitename.com/[COMPANY NAME]_medicine_[DISEASE].html
I am using Cakeph开发者_运维知识库p framework for development. Is there anyway to implement this url formatting in routes.php ? Or is there any other way ? please help.how about separating them with slashes?
// www.sitename.com/[COMPANY NAME]/medicine/[DISEASE]
Router::connect('/:company/medicine/:disease', array('controller' => 'diseases', 'action' => 'index'),
array('pass'=>array('company','disease'),
'company'=>"[a-zA-Z\.]+*",
'disease'=>'[a-zA-Z\.]+'));
and the controller
function diseases($company,$disease){
}
I'm not sure if you can use the underscore instead of the slashes, I have never tried it before. if you do try I'd like to know the results =)
Good Luck
EDITED: ok, i was too curious about this issue and i wrote a route like this
Router::connect('/:company_medicine_:disease', array('controller' => 'pages', 'action' => 'test'),
array('pass'=>array('company','disease'),
'company'=>'[a-zA-Z]+',
'disease'=>'[a-zA-Z]+'));
and its not working u_U
as i suspected, the problem is that Cake thinks that the name of the custom route element is :company_medicine
and not :company
.. after a few minutes regarding/reading the code of Cake i found out the exact place where Cake parses the route and extracts the names of the passed elements. It's in /cake/libs/router.php in the class CakeRoute, method _writeRoute() (about line 1369):
preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $parsed, $namedElements);
so as you can see in the regexp, the names of the elements can contain an underscore,therefore Cake thinks the name of the parameter is ":company_medicine". So you have four solutions:
- use slashes as separators for your urls
- change the order of your parameter so it would be
medicine_[COMPANY]_[DISEASE]
modify the line 1369 of the router.php to this (NOT RECOMENDED, i think it will break routes for plugins):
preg_match_all('#:([A-Za-z0-9]+)#', $parsed, $namedElements);
use url rewrite in your .htaccess to redirect all
[COMPANY]_medicine_[DISEASE]
to[COMPANY]/medicine/[DISEASE]
so cake will see it separated by slashes and the browser will see it separated bu underscores. (I haven't tested it, i've never added another rule to the .htaccess for Cake =P)
精彩评论