Markdown

[Symfony] How to get the get request query parameter

Access-Control-Allow-Origin:*
Cache-Control:no-cache, private
Connection:keep-alive
Content-Type:application/json
Date:Fri, 23 Jun 2017 07:44:50 GMT
Server:nginx/1.10.2
Transfer-Encoding:chunked
X-Powered-By:PHP/5.6.30
Request Headers
view parsed
GET /bank/search?{%22id%22:%221%22,%22behavior%22:%22deposit%22,%22from%22:%220%22,%22to%22:%22100%22} HTTP/1.1
Host: test123
Connection: keep-alive
Accept: text/plain, /; q=0.01
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4
Query String Parameters
view source
view URL encoded
{“id”:“1”,“behavior”:“deposit”,“from”:“0”,“to”:“100”}:

觀察一下標頭 可以發現參數是帶在URL後面那串
跟POST 不一樣 東西可以夾在content裡面
所以原本使用$request->getContent() 的方式就不管用了
應該根據不同的METHOD 換成以下方法

AJAX的部分也應該把DATA參數直接帶入,不能做josn化,因為會變成字串


use Symfony\Component\HttpFoundation\Request; public function updateAction(Request $request) { // $_GET parameters $request->query->get('name'); // $_POST parameters $request->request->get('name');

留言