Symfony Generaturl问题....必须匹配; (“我的特殊字符”字符串“给出)生成相应的URL。”

发布于 2025-01-29 22:27:15 字数 1427 浏览 1 评论 0原文

我正在尝试将Google索引器API与Symfony一起使用,因此我需要像在前端中已经使用的工作实用性(在数据库中)动态生成相同的URL。

我的控制器函数外观(减少)在我的前端中像这样

/**
 *
 * @Route({
 *     "de": "/profile/{name}-{id}/career/{jobname}-{jobid}",
 *     },  name="somename")
 */
public function detailfunction($name, $id, $jobname, $jobid)
{ // some code
}

我得到以下由twig(路径函数)呈现的URL:

https://www.mydomain.xy/profile/profile/this+s+s+compan+company+company+name-name-23/career/career/worker +montage+%2528MWD%2529-135

因此,现在我需要将完全相同的URL发送到Google,因此每当修改此页面时,它都会更新索引。

我尝试在这样的“ 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;

不幸的是,它输出“错误:参数“ Jobname” for route“ somename”必须匹配“ [^/] ++”(“ Worker Montage(M M Montage)(M /w/d)“给定)生成相应的URL”,

因此它不会编码来自URL生成器数据库的数据。我一直想知道Symfony(或树枝)实际上是如何在内部编码的,但我完全迷失了方向,非常感谢暗示。

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.

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):

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.

I try to generate this url in the controller of my "google indexer" function like this:

$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"

So it doesn´t encode the data that comes from the database for the url generator. I have been wondering how symfony (or twig as well) actually encodes internally but i am completely lost and very thankful for a hint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2025-02-05 22:27:15

尽管我仍然不确定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;
}

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.

  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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文