The Controller provides a glue between the domain model objects and transport layer events. Like the HTTP interface, Controllers are Request/Response oriented.
Controllers handle incoming HTTP requests, interact with the model to get data, and direct domain data to the Response object for display.
The full request object is injected via the action method and is primarily used to query for request parameters, whether they come from a GET or POST request, or from the URL. The request parameters are made available to the action through the accessor methods getParam(key, default_value) and getParams().
HTTP Request
GET /api/users/1/?api_key=value
Controller
class UsersController extends Controller { /** * GET /users/1.json * GET /users/1.xml */ public function showAction($request) { // only accept JSON and XML $request->acceptContentTypes(array('json', 'xml')); $response = new Response(); $response->id = $request->getParam('id'); $response->api_key = $request->getParam('api_key'); return $response; } }
You can customize instantiation using the init() method, which is called by the request object before calling the action method:
class UsersController extends Controller { public function init($request) { if (! $request->hasParam('api_key')) { // throwing or returning an Exception terminates the dispatch loop throw new Exception('Missing parameter: api_key', Response::FORBIDDEN); } if ('show' === $request->getMethod()) { // only accept JSON and XML $request->acceptContentTypes(array('json', 'xml')); } } public function showAction($request) { $response = new Response(); $response->id = $request->getParam('id'); $response->api_key = $request->getParam('api_key'); return $response; } }
Comments
Use this form to add corrections, additions and suggestions about the documentation on this page. If you encounter any problems, please use the GitHub issue tracker.