Commit 27a3efcd authored by Fabien Potencier's avatar Fabien Potencier

removed usage of the request service in the docs

parent b65d9315
...@@ -158,14 +158,16 @@ given. If the default is also missing, it will use an empty string. ...@@ -158,14 +158,16 @@ given. If the default is also missing, it will use an empty string.
You can now use this provider as follows:: You can now use this provider as follows::
use Symfony\Component\HttpFoundation\Request;
$app = new Silex\Application(); $app = new Silex\Application();
$app->register(new Acme\HelloServiceProvider(), array( $app->register(new Acme\HelloServiceProvider(), array(
'hello.default_name' => 'Igor', 'hello.default_name' => 'Igor',
)); ));
$app->get('/hello', function () use ($app) { $app->get('/hello', function (Request $request) use ($app) {
$name = $app['request']->get('name'); $name = $request->get('name');
return $app['hello']($name); return $app['hello']($name);
}); });
......
...@@ -51,6 +51,7 @@ The ``SerializerServiceProvider`` provider provides a ``serializer`` service: ...@@ -51,6 +51,7 @@ The ``SerializerServiceProvider`` provider provides a ``serializer`` service:
use Silex\Application; use Silex\Application;
use Silex\Provider\SerializerServiceProvider; use Silex\Provider\SerializerServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
$app = new Application(); $app = new Application();
...@@ -58,18 +59,18 @@ The ``SerializerServiceProvider`` provider provides a ``serializer`` service: ...@@ -58,18 +59,18 @@ The ``SerializerServiceProvider`` provider provides a ``serializer`` service:
$app->register(new SerializerServiceProvider()); $app->register(new SerializerServiceProvider());
// only accept content types supported by the serializer via the assert method. // only accept content types supported by the serializer via the assert method.
$app->get("/pages/{id}.{_format}", function ($id) use ($app) { $app->get("/pages/{id}.{_format}", function (Request $request, $id) use ($app) {
// assume a page_repository service exists that returns Page objects. The // assume a page_repository service exists that returns Page objects. The
// object returned has getters and setters exposing the state. // object returned has getters and setters exposing the state.
$page = $app['page_repository']->find($id); $page = $app['page_repository']->find($id);
$format = $app['request']->getRequestFormat(); $format = $request->getRequestFormat();
if (!$page instanceof Page) { if (!$page instanceof Page) {
$app->abort("No page found for id: $id"); $app->abort("No page found for id: $id");
} }
return new Response($app['serializer']->serialize($page, $format), 200, array( return new Response($app['serializer']->serialize($page, $format), 200, array(
"Content-Type" => $app['request']->getMimeType($format) "Content-Type" => $request->getMimeType($format)
)); ));
})->assert("_format", "xml|json") })->assert("_format", "xml|json")
->assert("id", "\d+"); ->assert("id", "\d+");
......
...@@ -62,11 +62,12 @@ Usage ...@@ -62,11 +62,12 @@ Usage
The Session provider provides a ``session`` service. Here is an example that The Session provider provides a ``session`` service. Here is an example that
authenticates a user and creates a session for them:: authenticates a user and creates a session for them::
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
$app->get('/login', function () use ($app) { $app->get('/login', function (Request $request) use ($app) {
$username = $app['request']->server->get('PHP_AUTH_USER', false); $username = $request->server->get('PHP_AUTH_USER', false);
$password = $app['request']->server->get('PHP_AUTH_PW'); $password = $request->server->get('PHP_AUTH_PW');
if ('igor' === $username && 'password' === $password) { if ('igor' === $username && 'password' === $password) {
$app['session']->set('user', array('username' => $username)); $app['session']->set('user', array('username' => $username));
......
...@@ -86,9 +86,9 @@ Usage ...@@ -86,9 +86,9 @@ Usage
The Swiftmailer provider provides a ``mailer`` service:: The Swiftmailer provider provides a ``mailer`` service::
$app->post('/feedback', function () use ($app) { use Symfony\Component\HttpFoundation\Request;
$request = $app['request'];
$app->post('/feedback', function (Request $request) use ($app) {
$message = \Swift_Message::newInstance() $message = \Swift_Message::newInstance()
->setSubject('[YourSite] Feedback') ->setSubject('[YourSite] Feedback')
->setFrom(array('noreply@yoursite.com')) ->setFrom(array('noreply@yoursite.com'))
......
...@@ -167,18 +167,17 @@ Core services ...@@ -167,18 +167,17 @@ Core services
Silex defines a range of services. Silex defines a range of services.
* **request**: Contains the current request object, which is an instance of * **request_stack**: Controls the lifecycle of requests, an instance of
`Request `RequestStack <http://api.symfony.com/master/Symfony/Component/HttpFoundation/RequestStack.html>` _.
<http://api.symfony.com/master/Symfony/Component/HttpFoundation/Request.html>`_.
It gives you access to ``GET``, ``POST`` parameters and lots more! It gives you access to ``GET``, ``POST`` parameters and lots more!
Example usage:: Example usage::
$id = $app['request']->get('id'); $id = $app['request_stack']->getCurrentRequest()->get('id');
This is only available when a request is being served; you can only access A request is only available when a request is being served; you can only
it from within a controller, an application before/after middlewares, or an access it from within a controller, an application before/after middlewares,
error handler. or an error handler.
* **routes**: The `RouteCollection * **routes**: The `RouteCollection
<http://api.symfony.com/master/Symfony/Component/Routing/RouteCollection.html>`_ <http://api.symfony.com/master/Symfony/Component/Routing/RouteCollection.html>`_
......
...@@ -760,8 +760,11 @@ Cross-Site-Scripting attacks. ...@@ -760,8 +760,11 @@ Cross-Site-Scripting attacks.
* **Escaping HTML**: PHP provides the ``htmlspecialchars`` function for this. * **Escaping HTML**: PHP provides the ``htmlspecialchars`` function for this.
Silex provides a shortcut ``escape`` method:: Silex provides a shortcut ``escape`` method::
$app->get('/name', function (Silex\Application $app) { use Symfony\Component\HttpFoundation\Request;
$name = $app['request']->get('name');
$app->get('/name', function (Request $request, Silex\Application $app) {
$name = $request->get('name');
return "You provided the name {$app->escape($name)}."; return "You provided the name {$app->escape($name)}.";
}); });
...@@ -771,8 +774,11 @@ Cross-Site-Scripting attacks. ...@@ -771,8 +774,11 @@ Cross-Site-Scripting attacks.
* **Escaping JSON**: If you want to provide data in JSON format you should * **Escaping JSON**: If you want to provide data in JSON format you should
use the Silex ``json`` function:: use the Silex ``json`` function::
$app->get('/name.json', function (Silex\Application $app) { use Symfony\Component\HttpFoundation\Request;
$name = $app['request']->get('name');
$app->get('/name.json', function (Request $request, Silex\Application $app) {
$name = $request->get('name');
return $app->json(array('name' => $name)); return $app->json(array('name' => $name));
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment