【问题标题】:In symfony request how do I get the uri path without the query params?在 symfony 请求中,如何在没有查询参数的情况下获取 uri 路径?
【发布时间】:2018-03-17 05:02:02
【问题描述】:
$request = new Symfony\Component\HttpFoundation\Request()
$request->getRequestUri();

似乎返回路径和查询参数。如何获得路径?

【问题讨论】:

标签: php symfony path request


【解决方案1】:

$request->getPathInfo() 就是你要找的东西。

【讨论】:

    【解决方案2】:
    strtok($request->getRequestUri(), '?');
    

    ...或...

    $request->getBaseUrl() . $request->getPathInfo();
    

    【讨论】:

      【解决方案3】:

      或者您也可以只使用 PHP:

      $urlWithoutQueryString = strtok($_SERVER["REQUEST_URI"], '?');
      

      【讨论】:

      • 抱歉,问题指定了 Symfony。
      【解决方案4】:

      以上方法在 Laravel 5.7 中都不适合我。我最终使用以下内容来获取不带查询字符串的完整请求 URI:

      $uri = strtok($request->getUri(), '?');
      

      这将返回 https://example.com/request/uri 以向 https://example.com/request/uri?param=1 发出请求。

      【讨论】:

        猜你喜欢
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 2021-01-02
        • 2022-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-30
        相关资源
        最近更新 更多