简体   繁体   English

如何在CakePHP的HtmlHelper#link(…)调用中重新定义URL生成而无需更改?

[英]How to redefine the URLs generation without changes at the HtmlHelper#link(…) calls in CakePHP?

I have a CakePHP website with many internal links, that are build with the HtmlHelper : 我有一个CakePHP网站,其中包含许多内部链接,这些链接是使用HtmlHelper构建的:

/app/View/MyController/myaction.ctp /app/View/MyController/myaction.ctp

<?php
echo $this->Html->link(
    $item['Search']['name'],
    array(
        'controller' => 'targetcontroller',
        'action' => 'targetaction',
        $profileId,
        $languageId
    )
);
?>

It works fine with the default route: 默认路由可以正常工作:

/app/Config/routes.php /app/Config/routes.php

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

The generated links look like that: /mycontroller/myaction/$profileId/$languageId . 生成的链接如下所示: /mycontroller/myaction/$profileId/$languageId

Now I want to use search engine friendly URLs (with profile names and ISO-639-1 language codes instead of IDs) for a part of the website and added a new Route: 现在,我要对网站的一部分使用搜索引擎友好的URL(使用配置文件名称和ISO-639-1语言代码代替ID),并添加新的Route:

/app/Config/routes.php /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 it also works fine and the incomming URIs like /producer/en/TestName.html are interpreted correctly. 而且它也可以正常工作,并且正确解释了诸如/producer/en/TestName.html类的/producer/en/TestName.html URI。

But the HtmlHelper is still generating the old URIs like /mycontroller/myaction/1/1 . 但是HtmlHelper仍在生成旧的URI,如/mycontroller/myaction/1/1

The docu says: 实况说:

Reverse routing is a feature in CakePHP that is used to allow you to easily change your URL structure without having to modify all your code. 反向路由是CakePHP的一项功能,可让您轻松更改URL结构,而无需修改所有代码。 By using routing arrays to define your URLs, you can later configure routes and the generated URLs will automatically update. 通过使用路由数组定义URL,以后可以配置路由,并且生成的URL将自动更新。

Well, the HtmlHelper gets a routing array as input, that means: I'm using the reverse routing. 好了, HtmlHelper获取一个路由数组作为输入,这意味着:我正在使用反向路由。

Why does it not work? 为什么不起作用? How to make the HtmlHelper generate the new URLs (without changing the HtmlHelper#link(...) calls)? 如何使HtmlHelper生成新的URL(而不更改HtmlHelper#link(...)调用)?

Bit of explanation first 先解释一下

You are technically not using reverse routing. 技术上讲,您不是在使用反向路由。 You see, the output link, /mycontroller/myaction/1/1 definitively doesn't match /iso/name.html . 您会看到,输出链接/mycontroller/myaction/1/1确实与/iso/name.html不匹配。 Like, in no way. 喜欢,绝不。 So, the routing skips that rule because it doesn't apply. 因此,路由会跳过该规则,因为它不适用。

Code

Try this 尝试这个

echo $this->Html->link(
    $item['Search']['name'],
    array(
        'controller' => 'targetcontroller',
        'action' => 'targetaction',
        'iso6391' => $someStringWithIso,
        'name' => $someName
    )
);

But for that, you have to change your routing a bit, because you are not passing the parameters (check the docs for 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 you have to mind the first string match /:iso6391/:name.html . 而且您必须注意第一个字符串匹配/:iso6391/:name.html Do you want to match this route to every controller and action in your project, or just the one controller and the one view . 您是否要将此路线与项目中的每个控制器和操作(或仅一个控制器和一个视图)进行匹配。 If it is for all projects, just for precaution, use this 如果它适用于所有项目,则仅出于预防目的,请使用此

  /:controller/:action/:iso6391/:name.html

if is just for, say, Controller1 and action "view", use 如果仅用于例如Controller1和操作“视图”,请使用

  /controller1/view/:iso6391/:name.html

The detail you need to consider is the extension you use .html , is that really necessary in the url? 您需要考虑的细节是使用.html的扩展名,网址中是否真的有必要? If it is, add it as a parameter in the Html#link 如果是这样,请将其添加为Html#link中的参数

echo $this->Html->link(
    $item['Search']['name'],
    array(
        'controller' => 'targetcontroller',
        'action' => 'targetaction',
        'iso6391' => $someStringWithIso,
        'name' => $someName

        'ext' => 'html'
    )
);

and also add parseExtensions to the routing file. 并将parseExtensions添加到路由文件中。 Read this . 阅读此 Would be easier if you don't add the extension, but that's up to you. 如果您不添加扩展名,将会更容易,但这取决于您。

In the end, you still have to change your calls to Html->link ... 最后,您仍然必须Html->link调用更改为...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM