8000 [quick tour] simplified "the controller" chapter · symfony/symfony-docs@eb3fe4c · GitHub
[go: up one dir, main page]

Skip to content

Commit eb3fe4c

Browse files
javiereguiluzweaverryan
authored andcommitted
[quick tour] simplified "the controller" chapter
1 parent dbbc8c2 commit eb3fe4c

File tree

1 file changed

+28
-106
lines changed

1 file changed

+28
-106
lines changed

quick_tour/the_controller.rst

Lines changed: 28 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ in Symfony2 is straightforward. Tweak the route by adding a default value of
2828
return array('name' => $name);
2929
}
3030

31-
By using the request format (as defined by the ``_format`` value), Symfony2
32-
automatically selects the right template, here ``hello.xml.twig``:
31+
By using the request format (as defined by the special ``_format`` variable),
32+
Symfony2 automatically selects the right template, here ``hello.xml.twig``:
3333

3434
.. code-block:: xml+php
3535

10000
@@ -41,7 +41,7 @@ automatically selects the right template, here ``hello.xml.twig``:
4141
That's all there is to it. For standard formats, Symfony2 will also
4242
automatically choose the best ``Content-Type`` header for the response. If
4343
you want to support different formats for a single action, use the ``{_format}``
44-
placeholder in the route path instead::
44+
variable in the route path instead::
4545

4646
// src/Acme/DemoBundle/Controller/DemoController.php
4747
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@@ -50,18 +50,23 @@ placeholder in the route path instead::
5050
// ...
5151

5252
/**
53-
* @Route("/hello/{name}.{_format}", defaults={"_format"="html"}, requirements={"_format"="html|xml|json"}, name="_demo_hello")
53+
* @Route(
54+
* "/hello/{name}.{_format}",
55+
* defaults = {"_format"="html"},
56+
* requirements = {"_format"="html|xml|json"},
57+
* name = "_demo_hello"
58+
* )
5459
* @Template()
5560
*/
5661
public function helloAction($name)
5762
{
5863
return array('name' => $name);
5964
}
6065

61-
The controller will now be called for URLs like ``/demo/hello/Fabien.xml`` or
66+
The controller will now match URLs like ``/demo/hello/Fabien.xml`` or
6267
``/demo/hello/Fabien.json``.
6368

64-
The ``requirements`` entry defines regular expressions that placeholders must
69+
The ``requirements`` entry defines regular expressions that variables must
6570
match. In this example, if you try to request the ``/demo/hello/Fabien.js``
6671
resource, you will get a 404 HTTP error, as it does not match the ``_format``
6772
requirement.
@@ -78,21 +83,16 @@ The ``generateUrl()`` is the same method as the ``path()`` function used in the
7883
templates. It takes the route name and an array of parameters as arguments and
7984
returns the associated friendly URL.
8085

81-
You can also easily forward the action to another one with the ``forward()``
82-
method. Internally, Symfony makes a "sub-request", and returns the ``Response``
83-
object from that sub-request::
84-
85-
$response = $this->forward('AcmeDemoBundle:Hello:fancy', array('name' => $name, 'color' => 'green'));
86+
You can also forward internally the action to another one with the ``forward()``
87+
method::
8688

87-
// ... do something with the response or return it directly
89+
return $this->forward('AcmeDemoBundle:Hello:fancy', array('name' => $name, 'color' => 'green'));
8890

8991
Getting information from the Request
9092
------------------------------------
9193

92-
Besides the values of the routing placeholders, the controller also has access
93-
to the ``Request`` object. The framework injects the ``Request`` object in the
94-
controller if a variable is type hinted with
95-
`Symfony\Component\HttpFoundation\Request`::
94+
Symfony automatically injects the ``Request`` object when the controller defines
95+
a variable type hinted with `Symfony\Component\HttpFoundation\Request`::
9696

9797
use Symfony\Component\HttpFoundation\Request;
9898

@@ -102,7 +102,7 @@ controller if a variable is type hinted with
102102

103103
$request->getPreferredLanguage(array('en', 'fr'));
104104

105-
$request->query->get('page'); // get a $_GET parameter
105+
$request->query->get('page'); // get a $_GET parameter
106106

107107
$request->request->get('page'); // get a $_POST parameter
108108
}
@@ -136,95 +136,26 @@ from any controller::
136136
// store an attribute for reuse during a later user request
137137
$session->set('foo', 'bar');
138138

139-
// in another controller for another request
139+
// get the value of a session attribute
140140
$foo = $session->get('foo');
141141

142-
// use a default value if the key doesn't exist
142+
// use a default value if the attribute doesn't exist
143143
$filters = $session->get('filters', array());
144144
}
145145

146-
You can also store small messages that will only be available for the very
147-
next request::
146+
You can also store "flash messages" that will auto-delete after the next request.
147+
They are useful for instance when you need to set a success message before
148+
redirecting the user to another page (which will then show the message)::
148149

149150
// store a message for the very next request (in a controller)
150151
$session->getFlashBag()->add('notice', 'Congratulations, your action succeeded!');
151152

152153
// display any messages back in the next request (in a template)
153154

154-
{% for flashMessage in app.session.flashbag.get('notice') %}
155-
<div>{{ flashMessage }}</div>
155+
{% if app.session.flashbag.has('notice') %}
156+
<div>{{ app.session.flashbag.get('notice') }}</div>
156157
{% endfor %}
157158

158-
This is useful when you need to set a success message before redirecting
159-
the user to another page (which will then show the message). Please note that
160-
when you use has() instead of get(), the flash message will not be cleared and
161-
thus remains available during the following requests.
162-
163-
Securing Resources
164-
------------------
165-
166-
The Symfony Standard Edition comes with a simple security configuration that
167-
fits most common needs:
168-
169-
.. code-block:: yaml
170-
171-
# app/config/security.yml
172-
security:
173-
encoders:
174-
Symfony\Component\Security\Core\User\User: plaintext
175-
176-
role_hierarchy:
177-
ROLE_ADMIN: ROLE_USER
178-
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
179-
180-
providers:
181-
in_memory:
182-
memory:
183-
users:
184-
user: { password: userpass, roles: [ 'ROLE_USER' ] }
185-
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
186-
187-
firewalls:
188-
dev:
189-
pattern: ^/(_(profiler|wdt)|css|images|js)/
190-
security: false
191-
192-
login:
193-
pattern: ^/demo/secured/login$
194-
security: false
195-
196-
secured_area:
197-
pattern: ^/demo/secured/
198-
form_login:
199-
check_path: /demo/secured/login_check
200-
login_path: /demo/secured/login
201-
logout:
202-
path: /demo/secured/logout
203-
target: /demo/
204-
205-
This configuration requires users to log in for any URL starting with
206-
``/demo/secured/`` and defines two valid users: ``user`` and ``admin``.
207-
Moreover, the ``admin`` user has a ``ROLE_ADMIN`` role, which includes the
208-
``ROLE_USER`` role as well (see the ``role_hierarchy`` setting).
209-
210-
.. tip::
211-
212-
For readability, passwords are stored in clear text in this simple
213-
configuration, but you can use any hashing algorithm by tweaking the
214-
``encoders`` section.
215-
216-
Going to the ``http://localhost/app_dev.php/demo/secured/hello``
217-
URL will automatically redirect you to the login form because this resource is
218-
protected by a ``firewall``.
219-
220-
.. note::
221-
222-
The Symfony2 security layer is very flexible and comes with many different
223-
user providers (like one for the Doctrine ORM) and authentication providers
224-
(like HTTP basic, HTTP digest, or X509 certificates). Read the
225-
":doc:`/book/security`" chapter of the book for more information
226-
on how to use and configure them.
227-
228159
Caching Resources
229160
-----------------
230161

@@ -247,19 +178,10 @@ convenient ``@Cache()`` annotation::
247178
return array('name' => $name);
248179
}
249180

250-
In this example, the resource will be cached for a day. But you can also use
251-
validation instead of expiration or a combination of both if that fits your
252-
needs better.
253-
254-
Resource caching is managed by the Symfony2 built-in reverse proxy. But because
255-
caching is managed using regular HTTP cache headers, you can replace the
256-
built-in reverse proxy with Varnish or Squid and easily scale your application.
257-
258-
.. note::
259-
260-
But what if you cannot cache whole pages? Symfony2 still has the solution
261-
via Edge Side Includes (ESI), which are supported natively. Learn more by
262-
reading the ":doc:`/book/http_cache`" chapter of the book.
181+
In this example, the resource will be cached for a day (``86400`` seconds).
182+
Resource caching is managed by Symfony2 itself. But because caching is managed
183+
using standard HTTP cache headers, you can use Varnish or Squid without having
184+
to modify a single line of code in your application.
263185

264186
Final Thoughts
265187
--------------

0 commit comments

Comments
 (0)
0