Controllers should be small and focus on handling HTTP requests: the actual business logic of your application should be delegated to different parts of your application, for instance your domain model.
// src/AppBundle/Controller/HelloWorldController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloWorldController
{
public function helloWorldAction()
{
return new Response(
'<html><body>Hello World!</body></html>'
);
}
}
Most of the time, you will want to render HTML responses from a template instead of hard-coding the HTML in your controller. Also, your templates will not be static but will contain placeholders for application data. By default Symfony comes with Twig, a powerful templating language.
In order to use Twig in your controller, extend Symfony's base Controller
class:
// src/AppBundle/Controller/HelloWorldController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloWorldController extends Controller
{
public function helloWorldAction()
{
$text = 'Hello World!';
return $this->render('hello-world.html.twig', ['text' => $text]);
}
}
Create the Twig template (located in app/Resources/views/hello-world.html.twig
):
<html><body>{{ text }}</body></html>
Twig will automatically replace the {{ text }}
placeholder with the value of the text
parameter, passed by the controller. This will render the following HTML output:
<html><body>Hello World!</body></html>
Sometimes you want to return a 404 (Not Found) response, because the requested resource does not exist. Symfony allows you to do so by throwing a NotFoundHttpException
.
The Symfony base Controller exposes a createNotFoundException
method which creates the exception for you:
public function indexAction()
{
// retrieve the object from database
$product = ...;
if (!$product) {
throw $this->createNotFoundException('The product does not exist');
}
// continue with the normal flow if no exception is thrown
return $this->render(...);
}
If you need to access the Request
object (for instance to read the query parameters, to read an HTTP header or to process an uploaded file), you can receive the request as a method argument by adding a type-hinted argument:
use Symfony\Component\HttpFoundation\Request;
public function indexAction(Request $request)
{
$queryParam = $request->query->get('param');
// ...
}
Symfony will recognize the type hint and add the request argument to the controller call.
route_name
with a placeholder