Symfony Controllers: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 4: | Line 4: | ||
A controller is a PHP callable you create that takes information from the HTTP request and constructs and returns an HTTP response (as a Symfony Response object).<ref>[http://symfony.com/doc/current/book/controller.html Symfony > Documentation > The Book > Controller]</ref> | A controller is a PHP callable you create that takes information from the HTTP request and constructs and returns an HTTP response (as a Symfony Response object).<ref>[http://symfony.com/doc/current/book/controller.html Symfony > Documentation > The Book > Controller]</ref> | ||
Typically, a controller is a method of a controller class:<ref>[http://symfony.com/doc/current/book/controller.html#a-simple-controller A Simple Controller], Symfony documentation</ref> | |||
<syntaxhighlight lang="php" highlight="6-7,9-10"> | |||
// src/AppBundle/Controller/HelloController.php | |||
namespace AppBundle\Controller; | |||
use Symfony\Component\HttpFoundation\Response; | |||
/** controller class... */ | |||
class HelloController | |||
{ | |||
/** ...and the methods are the various "controllers" */ | |||
public function indexAction($name) | |||
{ | |||
return new Response('<html><body>Hello '.$name.'!</body></html>'); | |||
} | |||
} | |||
</syntaxhighlight> | |||
== Requests, Controller, Response Lifecycle == | == Requests, Controller, Response Lifecycle == | ||
Latest revision as of 22:28, 27 January 2015
Overview[edit]
A controller is a PHP callable you create that takes information from the HTTP request and constructs and returns an HTTP response (as a Symfony Response object).[1]
Typically, a controller is a method of a controller class:[2]
// src/AppBundle/Controller/HelloController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
/** controller class... */
class HelloController
{
/** ...and the methods are the various "controllers" */
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
Requests, Controller, Response Lifecycle[edit]
Every request handled by a Symfony project goes through the same simple lifecycle. The framework takes care of all the repetitive stuff: you just need to write your custom code in the controller function:
- Each request is handled by a single front controller file (e.g.
app.phporapp_dev.php) that bootstraps the application; - The
Routerreads information from the request (e.g. the URI), finds a route that matches that information, and reads the_controllerparameter from the route; - The controller from the matched route is executed and the code inside the controller creates and returns a
Responseobject; - The HTTP headers and content of the
Responseobject are sent back to the client.[3]
Notes[edit]
- ↑ Symfony > Documentation > The Book > Controller
- ↑ A Simple Controller, Symfony documentation
- ↑ Requests, Controller, Response Lifecycle, Symfony documentation