代码之家  ›  专栏  ›  技术社区  ›  yevg

如何在symfony4中获取查询字符串

  •  1
  • yevg  · 技术社区  · 6 年前

    我正在尝试访问symfony4中的查询字符串参数

    namespace App\Controller;
    
    use Symfony\Component\HttpFoundation\RequestStack;
    
    class Home extends Controller {
    
        private $request;
    
        public function __construct(RequestStack $request){
    
            $this->request = $request;
        }
    
        public function getQueryString(){
    
           $req = $this->request->getCurrentRequest();
    
           print_r($req); // see all the request data
    
           // $req -> grab the query parameters
           // return query parameters
        }
    }
    

    我正在使用 RequestStack 当我打印结果时可以看到一堆请求数据 getCurrentRequest() (包括我需要的查询参数),但大多数方法 private 我无法访问它们。

    如何在Symfony中获取请求URL组件(包括查询参数)?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Imanali Mamadiev Ghazaleh Javaheri    6 年前

    为了 得到 查询:

    $this->request->getCurrentRequest()->query->get('name_query');
    

    为了 查询:

    $this->request->getCurrentRequest()->request->get('name_query');
    
        2
  •  0
  •   Francisco Luz    5 年前
    // retrieves $_GET and $_POST variables respectively
    $request->query->get('id');
    $request->request->get('category', 'default category');
    

    来源 https://symfony.com/doc/4.3/introduction/http_fundamentals.html#symfony-request-object