简体   繁体   English

Symfony generateUrl 问题 .... 必须匹配 "[^/]++" ("my string with special chars" given) 以生成相应的 URL。

[英]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.

相关问题 Symfony2“参数”“用于路由”“必须匹配”[^ /] ++“(”“给定)以生成相应的URL。” - Symfony2 “Parameter ”“ for route ”“ must match ”[^/]++“ (”“ given) to generate a corresponding URL.” Symfony2 允许空路由参数“必须匹配 ^/ ++(给定)以生成相应的 url” - Symfony2 allow empty route parameter “must match ^/ ++ ( given) to generate a corresponding url” 路径“”的参数“”必须与“ [^ /] ++”(给出的“”)匹配,以生成相应的URL - Parameter “” for route “” must match “[^/]++” (“” given) to generate a corresponding URL Symfony 4 - 键“_username”必须是字符串,给出“NULL” - Symfony 4 - The key "_username" must be a string, "NULL" given MySQL获得完全相同的字符串匹配,但键入不同的特殊字符 - Mysql get exact match of same string but typed with different special chars 从网址中删除特殊字符 - Remove special chars from URL 传递的参数必须是长文本的实例,给定的字符串 - doctrin2 symfony - Argument passed must be an instance of longtext, string given - doctrin2 symfony Symfony-MemcachedAdapter :: __ construct()必须是Memcached的实例,给定字符串 - Symfony - MemcachedAdapter::__construct() must be an instance of Memcached, string given PHP Symfony createFormBuilder::add 必须是字符串或 null 类型,数组给定 - PHP Symfony createFormBuilder ::add must be of the type string or null, array given Symfony2路线路径中的特殊字符 - Symfony2 special chars in route path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM