Search code examples
symfony-2.3

Symfony2 $request->query->get() returning null value


I'm new to Symfony2.I am understanding the framework. I try to access the get parameters of my request using Symfony2. But it is returning null when I access them like

$name = $request->query->get('name');
echo $name;

My code for controller is

namespace abc\myBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpFoundation\Request;

 public function getnameAction()
{

    $request = $this->get('request');
    echo $request->getMethod();

    $name = $request->query->get('name');
    echo $name. "---";

}

my routing file is as follows:

abcmy_newpage:
    pattern:  /new/{name}
    defaults: { _controller: abcmyBundle:new:getname }

When I run the URL

http://dashboardsmf.iiit.ac.in/web/app_dev.php/new/India

I get the method name "GET" corresponding to echo statement " echo $request->getMethod(); ".

But I get the null/"" blank value for the echo statement echo $name. "---";

I dont know where I have mistaken. Please help me.Thanks in advance.


Solution

  • Try to change you action to this:

    public function getnameAction($name) {
        echo $name;
    }
    

    Symfony binds parameters defined in routing file to parameter names in action method (see Symfony book chapter on controller parameters for more details).

    If you want to use $request->query to get your parameters it should be passed like this: http://http://dashboardsmf.iiit.ac.in/web/app_dev.php/new?name=India.