Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
f0d7d7c2
Commit
f0d7d7c2
authored
May 06, 2015
by
Dave Marshall
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basic documentation
parent
5407435c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
0 deletions
+52
-0
doc/usage.rst
doc/usage.rst
+52
-0
No files found.
doc/usage.rst
View file @
f0d7d7c2
...
@@ -539,6 +539,58 @@ early::
...
@@ -539,6 +539,58 @@ early::
return new Response(...);
return new Response(...);
});
});
View Handlers
-------------
View Handlers allow you to intercept a controller result and transform it before
it gets returned to the kernel.
To register a view handler, pass a callable (or string that can be resolved to a
callable) to the view method. The callable should accept some sort of result
from the controller::
$app->view(function (Response $response) {
$response->setContent('Set by view handler');
return $response;
});
View Handlers also receive the ``Request`` as its second argument,
making them a good candidate for basic content negotiation::
$app->view(function (array $data, Request $request) use ($app) {
$acceptHeader = $request->headers->get('Accept');
$bestFormat = $app['negotiator']->getBestFormat($acceptHeader, array('json', 'xml'));
if ('json' === $bestFormat) {
return new JsonResponse($data);
}
if ('xml' === $bestFormat) {
return $app['serializer.xml']->renderResponse($data);
}
return $data;
});
View Handlers will be examined in the order they are added to the application
and Silex will use type hints to determine if a view handler should be used for
the current result, continously using the return value of the last view handler
as the input for the next::
$app->view(function (JsonResponse $response) use ($app) {
$response = $app['response_signer']->sign($response);
return $response;
});
.. note::
You must ensure that Silex receives a ``Response`` or a string as the result of
the last view handler (or controller) to be run.
Redirects
Redirects
---------
---------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment