[英]Symfony generatUrl problem .... must match "[^/]++" ("my string with special chars" given) to generate a corresponding URL."
I am trying to use the google indexer api with symfony and therefore i need to generate the same URLs dynamically from my job-entity (in the database) like i am already using in the frontend.我正在尝试将 google indexer api 与 symfony 一起使用,因此我需要从我的工作实体(在数据库中)动态生成相同的 URL,就像我已经在前端使用的一样。
My controller function looks (reduced) like this我的控制器功能看起来(减少)像这样
/**
*
* @Route({
* "de": "/profile/{name}-{id}/career/{jobname}-{jobid}",
* }, name="somename")
*/
public function detailfunction($name, $id, $jobname, $jobid)
{ // some code
}
In my frontend i get the following url rendered by twig (path function):在我的前端,我得到了由 twig(路径函数)呈现的以下 url:
https://www.mydomain.xy/profile/This+is+a+company+name-23/career/Worker+Montage+%2528mwd%2529-135 https://www.mydomain.xy/profile/This+is+a+company+name-23/career/Worker+Montage+%2528mwd%2529-135
So now i need to send the exact same url to google so it updates the index whenever this page is modified.所以现在我需要向谷歌发送完全相同的 url,以便在修改此页面时更新索引。
I try to generate this url in the controller of my "google indexer" function like this:我尝试在我的“google indexer”函数的控制器中生成这个url,如下所示:
$job = $this->getDoctrine()->getRepository(Jobs::class)->findBy(....);
$url = $this->generateUrl('somename', array('name' => $job->getCompany()->getName(),
'id' => $job->getCompany()->getid(),
'jobname" => $job->getTitle(),
'jobid' => $job->getId()));
// Debug
echo $url;
Unfortunately it outputs "ERROR : Parameter "jobname" for route "somename" must match "[^/]++" ("Worker Montage (m/w/d)" given) to generate a corresponding URL"不幸的是,它输出“错误:路由“somename”的参数“jobname”必须匹配“[^/]++”(给出的“Worker Montage(m/w/d)”)以生成相应的 URL”
So it doesn´t encode the data that comes from the database for the url generator.因此,它不会对来自 url 生成器的数据库的数据进行编码。 I have been wondering how symfony (or twig as well) actually encodes internally but i am completely lost and very thankful for a hint.
我一直想知道 symfony(或 twig)实际上是如何在内部编码的,但我完全迷失了,非常感谢你的提示。
Although i am still not sure of how exactly symfony / twig encodes the url, i wrote a small workaround function and call it before passing it to the generateURL function.虽然我仍然不确定 symfony / twig 是如何对 url 进行编码的,但我编写了一个小变通函数并在将其传递给 generateURL 函数之前调用它。
array('name' => $this->urlcleaner($job->getCompany()->getName()),
'id' => $job->getCompany()->getId(),
private function urlcleaner($string){
$string = preg_replace('/[^A-Za-z0-9\-ığşçöüÖÇŞİıĞ()\/]/', ' ', $string);
$string = str_replace("/","",$string);
$string = str_replace("-","",$string);
$string = urlencode($string);
return $string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.