从generateUrl中提取路径的php symfony

huangapple go评论45阅读模式
英文:

Extract path from generateUrl php symfony

问题

我有一个getArticleUrl方法,返回$this->urlGenerator->generate($article);

在我的函数中,我只需要将查询参数附加到articleUrl

public function articleController(Request $request, string $uuid)
{
    $queryParams = $request->query->all();

    $articleUrl = $this->getArticleUrl($uuid);

    $redirectUrl = 以某种方式附加查询参数;

    return new RedirectResponse($redirectUrl, 301);
}

我尝试再次使用生成方法,但它不起作用,因为我认为它已经包含了域名,并且会重复它

$redirectUrl = $this->urlGenerator->generate($articleUrl, $queryParams, UrlGeneratorInterface::ABSOLUTE_URL);

或者也许我可以从$articleUrl中仅获取路径,并为新路径调用generate,这可能会起作用,但它没有

public function articleController(Request $request, string $uuid)
{
    $queryParams = $request->query->all();

    $articleUrl = $this->getArticleUrl($uuid);

    $parsed = parse_url($articleUrl);
    $path = $parsed['path'];

    if ($path) {
        $redirectUrl = $this->urlGenerator->generate($path, $queryParams);
    } else {
        $redirectUrl = $articleUrl;
    }

    return new RedirectResponse($redirectUrl, 301);
}
英文:

Pretty basic question and I am sorry for that but I haven't find the answer to my problem yet

I have an getArticleUrl method that returns $this->urlGenerator->generate($article);

In my function, all I need to do is to append the query params to the articleUrl

    public function articleController(Request $request, string $uuid)
    {
        $queryParams = $request->query->all();

        $articleUrl = $this->getArticleUrl($uuid); 

        $redirectUrl = append somehow the query params; 

        return new RedirectResponse($redirectUrl, 301);
    }

I tried to use the generate method again but it didn't work as I supposed it already has the domain in it and it will duplicate it

$redirectUrl = $this->urlGenerator->generate($articleUrl, $queryParams, UrlGeneratorInterface::ABSOLUTE_URL);

or perhaps I could get only the path from $articleUrl and call the generate for the new path and that could work, but it didn't

    public function articleController(Request $request, string $uuid)
    {
        $queryParams = $request->query->all();

        $articleUrl = $this->getArticleUrl($uuid);

        $parsed = parse_url($articleUrl);
        $path = $parsed['path'];

        if ($path) {
            $redirectUrl = $this->urlGenerator->generate($path, $queryParams);
        } else {
            $redirectUrl = $articleUrl;
        }

        return new RedirectResponse($redirectUrl, 301);
    }

答案1

得分: 1

你可以使用 http_build_query 函数。

更多信息请参考:https://www.php.net/manual/en/function.http-build-query.php

英文:

You could use http_build_query

public function articleController(Request $request, string $uuid)
{
    $queryParams = $request->query->all();

    $articleUrl = $this->getArticleUrl($uuid);

    $redirectUrl = $articleUrl . '?' . http_build_query($queryParams);

    return new RedirectResponse($redirectUrl, 301);
}

More Info: https://www.php.net/manual/en/function.http-build-query.php

huangapple
  • 本文由 发表于 2023年2月8日 22:47:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定