Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The controller must return a response

Tags:

symfony

I am new with Symfony 2.. I try something really basic.. I have just created a bundle with the command line et put this in my controller :

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\BrowserKit\Response;

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return new Response('<html><body>Hello '.$name.'!</body></html>');
    }
}

and I have a logicException with "the controler must return a response"

this is not what I am doing here ?

Thank u

PS : I add the routing.yml in app

acme_hello:
   resource: "@AcmeHelloBundle/Resources/config/routing.yml"
   prefix:   /

routing.yml in Resources

acme_hello_homepage:
   pattern:  /hello/{name}
   defaults: { _controller: AcmeHelloBundle:Default:index }
like image 695
Marc Avatar asked Feb 14 '13 15:02

Marc


People also ask

What is the difference between a controller and response?

The response could be an HTML page, JSON, XML, a file download, a redirect, a 404 error or anything else. The controller runs whatever arbitrary logic your application needs to render the content of a page.

What is a controller in Symfony?

In Symfony, a controller is usually a class method which is used to accept requests, and return a Response object. When mapped with a URL, a controller becomes accessible and its response can be viewed.

Does Symfony2 expect response object to be returned from controller action?

It worked without it on one server, but not another Symfony2 expects a Response object to be returned from a controller action. I'm guessing you probably want something like the following:

Is it possible to return a new response object?

You can also return a new Response object directly, eg return new Response ('Hello, world') if needed. See the documentation for more details. That works, I generated the entity and crud with the console and on CentOS is not working correcly with annotations.


1 Answers

The controller must return a Symfony\Component\HttpFoundation\Response instance, so you should have:

use Symfony\Component\HttpFoundation\Response;

instead of

use Symfony\Component\BrowserKit\Response;
like image 115
Juan Sosa Avatar answered Oct 30 '22 20:10

Juan Sosa