routing - How to redefine the URLs generation without changes at the HtmlHelper#link(...) calls in CakePHP? -
i have cakephp website many internal links, build htmlhelper
:
/app/view/mycontroller/myaction.ctp
<?php echo $this->html->link( $item['search']['name'], array( 'controller' => 'targetcontroller', 'action' => 'targetaction', $profileid, $languageid ) ); ?>
it works fine default route:
/app/config/routes.php
router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
the generated links that: /mycontroller/myaction/$profileid/$languageid
.
now want use search engine friendly urls (with profile names , iso-639-1 language codes instead of ids) part of website , added new route:
/app/config/routes.php
router::connect( '/:iso6391/:name.html', array('controller' => 'mycontroller', 'action' => 'myaction'), array( 'iso6391' => '[a-za-z]+', 'name' => '[0-9a-za-zäöüßÄÖÜ\-]+', ) );
and works fine , incomming uris /producer/en/testname.html
interpreted correctly.
but htmlhelper
still generating old uris /mycontroller/myaction/1/1
.
the docu says:
reverse routing feature in cakephp used allow change url structure without having modify code. using routing arrays define urls, can later configure routes , generated urls automatically update.
well, htmlhelper
gets routing array input, means: i'm using reverse routing.
why not work? how make htmlhelper
generate new urls (without changing htmlhelper#link(...)
calls)?
bit of explanation first
you technically not using reverse routing. see, output link, /mycontroller/myaction/1/1
definitively doesn't match /iso/name.html
. like, in no way. so, routing skips rule because doesn't apply.
code
try this
echo $this->html->link( $item['search']['name'], array( 'controller' => 'targetcontroller', 'action' => 'targetaction', 'iso6391' => $somestringwithiso, 'name' => $somename ) );
but that, have change routing bit, because not passing parameters (check the docs examples)
router::connect( '/:iso6391/:name.html', array('controller' => 'mycontroller', 'action' => 'myaction'), array( 'pass' => array('iso6391', 'name'), 'iso6391' => '[a-za-z]+', 'name' => '[0-9a-za-zäöüßÄÖÜ\-]+', ) );
and have mind first string match /:iso6391/:name.html
. want match route every controller , action in project, or the one controller , the 1 view. if projects, precaution, use this
/:controller/:action/:iso6391/:name.html
if for, say, controller1 , action "view", use
/controller1/view/:iso6391/:name.html
the detail need consider extension use .html
, necessary in url? if is, add parameter in html#link
echo $this->html->link( $item['search']['name'], array( 'controller' => 'targetcontroller', 'action' => 'targetaction', 'iso6391' => $somestringwithiso, 'name' => $somename 'ext' => 'html' ) );
and add parseextensions
routing file. read this. easier if don't add extension, that's you.
in end, still have change calls html->link
...
Comments
Post a Comment