1

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.