@@ -28,8 +28,8 @@ in Symfony2 is straightforward. Tweak the route by adding a default value of
28
28
return array('name' => $name);
29
29
}
30
30
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 ``:
33
33
34
34
.. code-block :: xml+php
35
35
10000
@@ -41,7 +41,7 @@ automatically selects the right template, here ``hello.xml.twig``:
41
41
That's all there is to it. For standard formats, Symfony2 will also
42
42
automatically choose the best ``Content-Type `` header for the response. If
43
43
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::
45
45
46
46
// src/Acme/DemoBundle/Controller/DemoController.php
47
47
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
@@ -50,18 +50,23 @@ placeholder in the route path instead::
50
50
// ...
51
51
52
52
/**
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
+ * )
54
59
* @Template()
55
60
*/
56
61
public function helloAction($name)
57
62
{
58
63
return array('name' => $name);
59
64
}
60
65
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
62
67
``/demo/hello/Fabien.json ``.
63
68
64
- The ``requirements `` entry defines regular expressions that placeholders must
69
+ The ``requirements `` entry defines regular expressions that variables must
65
70
match. In this example, if you try to request the ``/demo/hello/Fabien.js ``
66
71
resource, you will get a 404 HTTP error, as it does not match the ``_format ``
67
72
requirement.
@@ -78,21 +83,16 @@ The ``generateUrl()`` is the same method as the ``path()`` function used in the
78
83
templates. It takes the route name and an array of parameters as arguments and
79
84
returns the associated friendly URL.
80
85
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::
86
88
87
- // ... do something with the response or return it directly
89
+ return $this->forward('AcmeDemoBundle:Hello:fancy', array('name' => $name, 'color' => 'green'));
88
90
89
91
Getting information from the Request
90
92
------------------------------------
91
93
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 `::
96
96
97
97
use Symfony\Component\HttpFoundation\Request;
98
98
@@ -102,7 +102,7 @@ controller if a variable is type hinted with
102
102
103
103
$request->getPreferredLanguage(array('en', 'fr'));
104
104
105
- $request->query->get('page'); // get a $_GET parameter
105
+ $request->query->get('page'); // get a $_GET parameter
106
106
107
107
$request->request->get('page'); // get a $_POST parameter
108
108
}
@@ -136,95 +136,26 @@ from any controller::
136
136
// store an attribute for reuse during a later user request
137
137
$session->set('foo', 'bar');
138
138
139
- // in another controller for another request
139
+ // get the value of a session attribute
140
140
$foo = $session->get('foo');
141
141
142
- // use a default value if the key doesn't exist
142
+ // use a default value if the attribute doesn't exist
143
143
$filters = $session->get('filters', array());
144
144
}
145
145
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)::
148
149
149
150
// store a message for the very next request (in a controller)
150
151
$session->getFlashBag()->add('notice', 'Congratulations, your action succeeded!');
151
152
152
153
// display any messages back in the next request (in a template)
153
154
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>
156
157
{% endfor %}
157
158
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
-
228
159
Caching Resources
229
160
-----------------
230
161
@@ -247,19 +178,10 @@ convenient ``@Cache()`` annotation::
247
178
return array('name' => $name);
248
179
}
249
180
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.
263
185
264
186
Final Thoughts
265
187
--------------
0 commit comments