@@ -61,19 +61,21 @@ cache.
61
61
.. index ::
62
62
single: Page creation; Example
63
63
64
- The "Hello Symfony! " Page
65
- -------------------------
64
+ The "Random Number " Page
65
+ ------------------------
66
66
67
- Start by building a spin-off of the classic "Hello World!" application. When
68
- you're finished, the user will be able to get a personal greeting (e.g. "Hello Symfony")
69
- by going to the following URL:
67
+ Instead of building the classic "Hello World!" application, this chapter develops
68
+ an appllication that generates random numbers . When you're finished, the user
69
+ will be able to get a random number between ``1 `` and the upper limit set with
70
+ the URL:
70
71
71
72
.. code-block :: text
72
73
73
- http://localhost/app_dev.php/hello/Symfony
74
+ http://localhost/app_dev.php/random/100
74
75
75
- Actually, you'll be able to replace ``Symfony `` with any other name to be
76
- greeted. To create the page, follow the simple two-step process.
76
+ Actually, you'll be able to replace ``100 `` with any other number to generate
77
+ numbers up to that upper limit. To create the page, follow the simple two-step
78
+ process.
77
79
78
80
.. note ::
79
81
@@ -97,15 +99,15 @@ A bundle is nothing more than a directory that houses everything related
97
99
to a specific feature, including PHP classes, configuration, and even stylesheets
98
100
and JavaScript files (see :ref: `page-creation-bundles `).
99
101
100
- To create a bundle called ``AcmeHelloBundle `` (a play bundle that you'll
102
+ To create a bundle called ``AcmeWebsiteBundle `` (a play bundle that you'll
101
103
build in this chapter), run the following command and follow the on-screen
102
104
instructions (use all of the default options):
103
105
104
106
.. code-block :: bash
105
107
106
- $ php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml
108
+ $ php app/console generate:bundle --namespace=Acme/WebsiteBundle --format=yml
107
109
108
- Behind the scenes, a directory is created for the bundle at ``src/Acme/HelloBundle ``.
110
+ Behind the scenes, a directory is created for the bundle at ``src/Acme/WebsiteBundle ``.
109
111
A line is also automatically added to the ``app/AppKernel.php `` file so that
110
112
the bundle is registered with the kernel::
111
113
@@ -114,7 +116,7 @@ the bundle is registered with the kernel::
114
116
{
115
117
$bundles = array(
116
118
...,
117
- new Acme\HelloBundle\AcmeHelloBundle (),
119
+ new Acme\WebsiteBundle\AcmeWebsiteBundle (),
118
120
);
119
121
// ...
120
122
@@ -132,15 +134,15 @@ located at ``app/config/routing.yml``. Like all configuration in Symfony2,
132
134
you can also choose to use XML or PHP out of the box to configure routes.
133
135
134
136
If you look at the main routing file, you'll see that Symfony already added
135
- an entry when you generated the ``AcmeHelloBundle ``:
137
+ an entry when you generated the ``AcmeWebsiteBundle ``:
136
138
137
139
.. configuration-block ::
138
140
139
141
.. code-block :: yaml
140
142
141
143
# app/config/routing.yml
142
- acme_hello :
143
- resource : " @AcmeHelloBundle /Resources/config/routing.yml"
144
+ acme_website :
145
+ resource : " @AcmeWebsiteBundle /Resources/config/routing.yml"
144
146
prefix : /
145
147
146
148
.. code-block :: xml
@@ -152,7 +154,7 @@ an entry when you generated the ``AcmeHelloBundle``:
152
154
xsi : schemaLocation =" http://symfony.com/schema/routing
153
155
http://symfony.com/schema/routing/routing-1.0.xsd" >
154
156
155
- <import resource =" @AcmeHelloBundle /Resources/config/routing.xml"
157
+ <import resource =" @AcmeWebsiteBundle /Resources/config/routing.xml"
156
158
prefix =" /" />
157
159
</routes >
158
160
@@ -164,14 +166,14 @@ an entry when you generated the ``AcmeHelloBundle``:
164
166
165
167
$collection = new RouteCollection();
166
168
$collection->addCollection(
167
- $loader->import('@AcmeHelloBundle /Resources/config/routing.php'),
169
+ $loader->import('@AcmeWebsiteBundle /Resources/config/routing.php'),
168
170
'/'
169
171
);
170
172
171
173
return $collection;
172
174
173
175
This entry is pretty basic: it tells Symfony to load routing configuration
174
- from the ``Resources/config/routing.yml `` file that lives inside the ``AcmeHelloBundle ``.
176
+ from the ``Resources/config/routing.yml `` file that lives inside the ``AcmeWebsiteBundle ``.
175
177
This means that you place routing configuration directly in ``app/config/routing.yml ``
176
178
or organize your routes throughout your application, and import them from here.
177
179
@@ -182,45 +184,45 @@ the new route that defines the URL of the page that you're about to create:
182
184
183
185
.. code-block :: yaml
184
186
185
- # src/Acme/HelloBundle /Resources/config/routing.yml
186
- hello :
187
- path : /hello/{name }
188
- defaults : { _controller: AcmeHelloBundle:Hello :index }
187
+ # src/Acme/WebsiteBundle /Resources/config/routing.yml
188
+ random :
189
+ path : /random/{limit }
190
+ defaults : { _controller: AcmeWebsiteBundle:Random :index }
189
191
190
192
.. code-block :: xml
191
193
192
- <!-- src/Acme/HelloBundle /Resources/config/routing.xml -->
194
+ <!-- src/Acme/WebsiteBundle /Resources/config/routing.xml -->
193
195
<?xml version =" 1.0" encoding =" UTF-8" ?>
194
196
<routes xmlns =" http://symfony.com/schema/routing"
195
197
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
196
198
xsi : schemaLocation =" http://symfony.com/schema/routing
197
199
http://symfony.com/schema/routing/routing-1.0.xsd" >
198
200
199
- <route id =" hello " path =" /hello/{name }" >
200
- <default key =" _controller" >AcmeHelloBundle:Hello :index</default >
201
+ <route id =" random " path =" /random/{limit }" >
202
+ <default key =" _controller" >AcmeWebsiteBundle:Random :index</default >
201
203
</route >
202
204
</routes >
203
205
204
206
.. code-block :: php
205
207
206
- // src/Acme/HelloBundle /Resources/config/routing.php
208
+ // src/Acme/WebsiteBundle /Resources/config/routing.php
207
209
use Symfony\Component\Routing\RouteCollection;
208
210
use Symfony\Component\Routing\Route;
209
211
210
212
$collection = new RouteCollection();
211
- $collection->add('hello ', new Route('/hello/{name }', array(
212
- '_controller' => 'AcmeHelloBundle:Hello :index',
213
+ $collection->add('random ', new Route('/random/{limit }', array(
214
+ '_controller' => 'AcmeWebsiteBundle:Random :index',
213
215
)));
214
216
215
217
return $collection;
216
218
217
219
The routing consists of two basic pieces: the ``path ``, which is the URL
218
220
that this route will match, and a ``defaults `` array, which specifies the
219
221
controller that should be executed. The placeholder syntax in the path
220
- (``{name } ``) is a wildcard. It means that ``/hello/Ryan ``, ``/hello/Fabien ``
221
- or any other similar URL will match this route. The ``{name } `` placeholder
222
+ (``{limit } ``) is a wildcard. It means that ``/number/10 ``, ``/number/327 ``
223
+ or any other similar URL will match this route. The ``{limit } `` placeholder
222
224
parameter will also be passed to the controller so that you can use its value
223
- to personally greet the user .
225
+ to generate the proper random number .
224
226
225
227
.. note ::
226
228
@@ -231,20 +233,20 @@ to personally greet the user.
231
233
Step 2: Create the Controller
232
234
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233
235
234
- When a URL such as ``/hello/Ryan `` is handled by the application, the ``hello ``
235
- route is matched and the ``AcmeHelloBundle:Hello :index `` controller is executed
236
+ When a URL such as ``/random/10 `` is handled by the application, the ``random ``
237
+ route is matched and the ``AcmeWebsiteBundle:Random :index `` controller is executed
236
238
by the framework. The second step of the page-creation process is to create
237
239
that controller.
238
240
239
- The controller - ``AcmeHelloBundle:Hello :index `` is the *logical * name of
241
+ The controller - ``AcmeWebsiteBundle:Random :index `` is the *logical * name of
240
242
the controller, and it maps to the ``indexAction `` method of a PHP class
241
- called ``Acme\HelloBundle \Controller\HelloController ``. Start by creating this file
242
- inside your ``AcmeHelloBundle ``::
243
+ called ``Acme\WebsiteBundle \Controller\RandomController ``. Start by creating this
244
+ file inside your ``AcmeWebsiteBundle ``::
243
245
244
- // src/Acme/HelloBundle /Controller/HelloController .php
245
- namespace Acme\HelloBundle \Controller;
246
+ // src/Acme/WebsiteBundle /Controller/RandomController .php
247
+ namespace Acme\WebsiteBundle \Controller;
246
248
247
- class HelloController
249
+ class RandomController
248
250
{
249
251
}
250
252
@@ -254,19 +256,19 @@ to build and prepare the resource being requested. Except in some advanced
254
256
cases, the end product of a controller is always the same: a Symfony2 ``Response ``
255
257
object.
256
258
257
- Create the ``indexAction `` method that Symfony will execute when the ``hello ``
259
+ Create the ``indexAction `` method that Symfony will execute when the ``random ``
258
260
route is matched::
259
261
260
- // src/Acme/HelloBundle /Controller/HelloController .php
261
- namespace Acme\HelloBundle \Controller;
262
+ // src/Acme/WebsiteBundle /Controller/RandomController .php
263
+ namespace Acme\WebsiteBundle \Controller;
262
264
263
265
use Symfony\Component\HttpFoundation\Response;
264
266
265
- class HelloController
267
+ class RandomController
266
268
{
267
- public function indexAction($name )
269
+ public function indexAction($limit )
268
270
{
269
- return new Response('<html><body>Hello '.$name.'! </body></html>');
271
+ return new Response('<html><body>Number: '.rand(1, $limit).' </body></html>');
270
272
}
271
273
}
272
274
@@ -276,11 +278,11 @@ page in this example).
276
278
277
279
Congratulations! After creating only a route and a controller, you already
278
280
have a fully-functional page! If you've setup everything correctly, your
279
- application should greet you:
281
+ application should generate a random number for you:
280
282
281
283
.. code-block :: text
282
284
283
- http://localhost/app_dev.php/hello/Ryan
285
+ http://localhost/app_dev.php/random/10
284
286
285
287
.. _book-page-creation-prod-cache-clear :
286
288
@@ -291,7 +293,7 @@ application should greet you:
291
293
292
294
.. code-block :: text
293
295
294
- http://localhost/app.php/hello/Ryan
296
+ http://localhost/app.php/random/10
295
297
296
298
If you get an error, it's likely because you need to clear your cache
297
299
by running:
@@ -318,24 +320,26 @@ of writing the HTML inside the controller, render a template instead:
318
320
.. code-block :: php
319
321
:linenos:
320
322
321
- // src/Acme/HelloBundle /Controller/HelloController .php
322
- namespace Acme\HelloBundle \Controller;
323
+ // src/Acme/WebsiteBundle /Controller/RandomController .php
324
+ namespace Acme\WebsiteBundle \Controller;
323
325
324
326
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
325
327
326
- class HelloController extends Controller
328
+ class RandomController extends Controller
327
329
{
328
- public function indexAction($name )
330
+ public function indexAction($limit )
329
331
{
332
+ $number = rand(1, $limit);
333
+
330
334
return $this->render(
331
- 'AcmeHelloBundle:Hello :index.html.twig',
332
- array('name ' => $name )
335
+ 'AcmeWebsiteBundle:Random :index.html.twig',
336
+ array('number ' => $number )
333
337
);
334
338
335
339
// render a PHP template instead
336
340
// return $this->render(
337
- // 'AcmeHelloBundle:Hello :index.html.php',
338
- // array('name ' => $name )
341
+ // 'AcmeWebsiteBundle:Random :index.html.php',
342
+ // array('number ' => $number )
339
343
// );
340
344
}
341
345
}
@@ -358,7 +362,7 @@ By default, Symfony2 supports two different templating languages: classic
358
362
PHP templates and the succinct but powerful `Twig `_ templates. Don't be
359
363
alarmed - you're free to choose either or even both in the same project.
360
364
361
- The controller renders the ``AcmeHelloBundle:Hello :index.html.twig `` template,
365
+ The controller renders the ``AcmeWebsiteBundle:Random :index.html.twig `` template,
362
366
which uses the following naming convention:
363
367
364
368
**BundleName **:**ControllerName **:**TemplateName **
@@ -368,27 +372,27 @@ location using the following convention.
368
372
369
373
**/path/to/BundleName **/Resources/views/**ControllerName **/**TemplateName **
370
374
371
- In this case, ``AcmeHelloBundle `` is the bundle name, ``Hello `` is the
375
+ In this case, ``AcmeWebsiteBundle `` is the bundle name, ``Random `` is the
372
376
controller, and ``index.html.twig `` the template:
373
377
374
378
.. configuration-block ::
375
379
376
380
.. code-block :: jinja
377
381
:linenos:
378
382
379
- {# src/Acme/HelloBundle /Resources/views/Hello /index.html.twig #}
383
+ {# src/Acme/WebsiteBundle /Resources/views/Random /index.html.twig #}
380
384
{% extends '::base.html.twig' %}
381
385
382
386
{% block body %}
383
- Hello {{ name }}!
387
+ Number: {{ number }}
384
388
{% endblock %}
385
389
386
390
.. code-block :: html+php
387
391
388
- <!-- src/Acme/HelloBundle /Resources/views/Hello /index.html.php -->
392
+ <!-- src/Acme/WebsiteBundle /Resources/views/Random /index.html.php -->
389
393
<?php $view->extend('::base.html.php') ?>
390
394
391
- Hello <?php echo $view->escape($name ) ?>!
395
+ Number: <?php echo $view->escape($number ) ?>
392
396
393
397
Step through the Twig template line-by-line:
394
398
@@ -414,7 +418,7 @@ and in the ``app`` directory:
414
418
<html>
415
419
<head>
416
420
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
417
- <title>{% block title %}Welcome! {% endblock %}</title>
421
+ <title>{% block title %}Random Number Generator {% endblock %}</title>
418
422
{% block stylesheets %}{% endblock %}
419
423
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
420
424
</head>
@@ -431,7 +435,7 @@ and in the ``app`` directory:
431
435
<html>
432
436
<head>
433
437
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
434
- <title><?php $view['slots']->output('title', 'Welcome! ') ?></title>
438
+ <title><?php $view['slots']->output('title', 'Random Number Generator ') ?></title>
435
439
<?php $view['slots']->output('stylesheets') ?>
436
440
<link rel="shortcut icon" href="<?php echo $view['assets']->getUrl('favicon.ico') ?>" />
437
441
</head>
@@ -509,16 +513,16 @@ use a Kernel class, ``AppKernel``, to bootstrap the application.
509
513
510
514
.. code-block :: text
511
515
512
- http://localhost/app.php/hello/Ryan
516
+ http://localhost/app.php/random/10
513
517
514
518
The front controller, ``app.php ``, is executed and the "internal:" URL
515
- ``/hello/Ryan `` is routed internally using the routing configuration.
519
+ ``/random/10 `` is routed internally using the routing configuration.
516
520
By using Apache ``mod_rewrite `` rules, you can force the ``app.php `` file
517
521
to be executed without needing to specify it in the URL:
518
522
519
523
.. code-block :: text
520
524
521
- http://localhost/hello/Ryan
525
+ http://localhost/random/10
522
526
523
527
Though front controllers are essential in handling every request, you'll
524
528
rarely need to modify or even think about them. They'll be mentioned again
@@ -570,9 +574,9 @@ You'll learn more about each of these directories in later chapters.
570
574
.. code-block :: text
571
575
572
576
Class Name:
573
- Acme\HelloBundle \Controller\HelloController
577
+ Acme\WebsiteBundle \Controller\RandomController
574
578
Path:
575
- src/Acme/HelloBundle /Controller/HelloController .php
579
+ src/Acme/WebsiteBundle /Controller/RandomController .php
576
580
577
581
The Source (``src ``) Directory
578
582
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -722,10 +726,10 @@ Bundle Directory Structure
722
726
723
727
The directory structure of a bundle is simple and flexible. By default, the
724
728
bundle system follows a set of conventions that help to keep code consistent
725
- between all Symfony2 bundles. Take a look at ``AcmeHelloBundle ``, as it contains
729
+ between all Symfony2 bundles. Take a look at ``AcmeWebsiteBundle ``, as it contains
726
730
some of the most common elements of a bundle:
727
731
728
- * ``Controller/ `` contains the controllers of the bundle (e.g. ``HelloController .php ``);
732
+ * ``Controller/ `` contains the controllers of the bundle (e.g. ``RandomController .php ``);
729
733
730
734
* ``DependencyInjection/ `` holds certain dependency injection extension classes,
731
735
which may import service configuration, register compiler passes or more
@@ -907,14 +911,14 @@ the application via the development front controller:
907
911
908
912
.. code-block :: text
909
913
910
- http://localhost/app_dev.php/hello/Ryan
914
+ http://localhost/app_dev.php/random/10
911
915
912
916
If you'd like to see how your application will behave in the production environment,
913
917
call the ``prod `` front controller instead:
914
918
915
919
.. code-block :: text
916
920
917
- http://localhost/app.php/hello/Ryan
921
+ http://localhost/app.php/random/10
918
922
919
923
Since the ``prod `` environment is optimized for speed; the configuration,
920
924
routing and Twig templates are compiled into flat PHP classes and cached.
0 commit comments