Skip to content

Commit 575bc9a

Browse files
committed
Replaced the "Hello World" example to avoid confusion with AcmeDemoBundle
1 parent bffe163 commit 575bc9a

File tree

1 file changed

+77
-73
lines changed

1 file changed

+77
-73
lines changed

book/page_creation.rst

Lines changed: 77 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,21 @@ cache.
6161
.. index::
6262
single: Page creation; Example
6363

64-
The "Hello Symfony!" Page
65-
-------------------------
64+
The "Random Number" Page
65+
------------------------
6666

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:
7071

7172
.. code-block:: text
7273
73-
http://localhost/app_dev.php/hello/Symfony
74+
http://localhost/app_dev.php/random/100
7475
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.
7779

7880
.. note::
7981

@@ -97,15 +99,15 @@ A bundle is nothing more than a directory that houses everything related
9799
to a specific feature, including PHP classes, configuration, and even stylesheets
98100
and JavaScript files (see :ref:`page-creation-bundles`).
99101

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
101103
build in this chapter), run the following command and follow the on-screen
102104
instructions (use all of the default options):
103105

104106
.. code-block:: bash
105107
106-
$ php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml
108+
$ php app/console generate:bundle --namespace=Acme/WebsiteBundle --format=yml
107109
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``.
109111
A line is also automatically added to the ``app/AppKernel.php`` file so that
110112
the bundle is registered with the kernel::
111113

@@ -114,7 +116,7 @@ the bundle is registered with the kernel::
114116
{
115117
$bundles = array(
116118
...,
117-
new Acme\HelloBundle\AcmeHelloBundle(),
119+
new Acme\WebsiteBundle\AcmeWebsiteBundle(),
118120
);
119121
// ...
120122

@@ -132,15 +134,15 @@ located at ``app/config/routing.yml``. Like all configuration in Symfony2,
132134
you can also choose to use XML or PHP out of the box to configure routes.
133135

134136
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``:
136138

137139
.. configuration-block::
138140

139141
.. code-block:: yaml
140142
141143
# app/config/routing.yml
142-
acme_hello:
143-
resource: "@AcmeHelloBundle/Resources/config/routing.yml"
144+
acme_website:
145+
resource: "@AcmeWebsiteBundle/Resources/config/routing.yml"
144146
prefix: /
145147
146148
.. code-block:: xml
@@ -152,7 +154,7 @@ an entry when you generated the ``AcmeHelloBundle``:
152154
xsi:schemaLocation="http://symfony.com/schema/routing
153155
http://symfony.com/schema/routing/routing-1.0.xsd">
154156
155-
<import resource="@AcmeHelloBundle/Resources/config/routing.xml"
157+
<import resource="@AcmeWebsiteBundle/Resources/config/routing.xml"
156158
prefix="/" />
157159
</routes>
158160
@@ -164,14 +166,14 @@ an entry when you generated the ``AcmeHelloBundle``:
164166
165167
$collection = new RouteCollection();
166168
$collection->addCollection(
167-
$loader->import('@AcmeHelloBundle/Resources/config/routing.php'),
169+
$loader->import('@AcmeWebsiteBundle/Resources/config/routing.php'),
168170
'/'
169171
);
170172
171173
return $collection;
172174
173175
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``.
175177
This means that you place routing configuration directly in ``app/config/routing.yml``
176178
or organize your routes throughout your application, and import them from here.
177179

@@ -182,45 +184,45 @@ the new route that defines the URL of the page that you're about to create:
182184

183185
.. code-block:: yaml
184186
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 }
189191
190192
.. code-block:: xml
191193
192-
<!-- src/Acme/HelloBundle/Resources/config/routing.xml -->
194+
<!-- src/Acme/WebsiteBundle/Resources/config/routing.xml -->
193195
<?xml version="1.0" encoding="UTF-8" ?>
194196
<routes xmlns="http://symfony.com/schema/routing"
195197
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
196198
xsi:schemaLocation="http://symfony.com/schema/routing
197199
http://symfony.com/schema/routing/routing-1.0.xsd">
198200
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>
201203
</route>
202204
</routes>
203205
204206
.. code-block:: php
205207
206-
// src/Acme/HelloBundle/Resources/config/routing.php
208+
// src/Acme/WebsiteBundle/Resources/config/routing.php
207209
use Symfony\Component\Routing\RouteCollection;
208210
use Symfony\Component\Routing\Route;
209211
210212
$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',
213215
)));
214216
215217
return $collection;
216218
217219
The routing consists of two basic pieces: the ``path``, which is the URL
218220
that this route will match, and a ``defaults`` array, which specifies the
219221
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
222224
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.
224226

225227
.. note::
226228

@@ -231,20 +233,20 @@ to personally greet the user.
231233
Step 2: Create the Controller
232234
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233235

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
236238
by the framework. The second step of the page-creation process is to create
237239
that controller.
238240

239-
The controller - ``AcmeHelloBundle:Hello:index`` is the *logical* name of
241+
The controller - ``AcmeWebsiteBundle:Random:index`` is the *logical* name of
240242
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``::
243245

244-
// src/Acme/HelloBundle/Controller/HelloController.php
245-
namespace Acme\HelloBundle\Controller;
246+
// src/Acme/WebsiteBundle/Controller/RandomController.php
247+
namespace Acme\WebsiteBundle\Controller;
246248

247-
class HelloController
249+
class RandomController
248250
{
249251
}
250252

@@ -254,19 +256,19 @@ to build and prepare the resource being requested. Except in some advanced
254256
cases, the end product of a controller is always the same: a Symfony2 ``Response``
255257
object.
256258

257-
Create the ``indexAction`` method that Symfony will execute when the ``hello``
259+
Create the ``indexAction`` method that Symfony will execute when the ``random``
258260
route is matched::
259261

260-
// src/Acme/HelloBundle/Controller/HelloController.php
261-
namespace Acme\HelloBundle\Controller;
262+
// src/Acme/WebsiteBundle/Controller/RandomController.php
263+
namespace Acme\WebsiteBundle\Controller;
262264

263265
use Symfony\Component\HttpFoundation\Response;
264266

265-
class HelloController
267+
class RandomController
266268
{
267-
public function indexAction($name)
269+
public function indexAction($limit)
268270
{
269-
return new Response('<html><body>Hello '.$name.'!</body></html>');
271+
return new Response('<html><body>Number: '.rand(1, $limit).'</body></html>');
270272
}
271273
}
272274

@@ -276,11 +278,11 @@ page in this example).
276278

277279
Congratulations! After creating only a route and a controller, you already
278280
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:
280282

281283
.. code-block:: text
282284
283-
http://localhost/app_dev.php/hello/Ryan
285+
http://localhost/app_dev.php/random/10
284286
285287
.. _book-page-creation-prod-cache-clear:
286288

@@ -291,7 +293,7 @@ application should greet you:
291293

292294
.. code-block:: text
293295
294-
http://localhost/app.php/hello/Ryan
296+
http://localhost/app.php/random/10
295297
296298
If you get an error, it's likely because you need to clear your cache
297299
by running:
@@ -318,24 +320,26 @@ of writing the HTML inside the controller, render a template instead:
318320
.. code-block:: php
319321
:linenos:
320322
321-
// src/Acme/HelloBundle/Controller/HelloController.php
322-
namespace Acme\HelloBundle\Controller;
323+
// src/Acme/WebsiteBundle/Controller/RandomController.php
324+
namespace Acme\WebsiteBundle\Controller;
323325
324326
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
325327
326-
class HelloController extends Controller
328+
class RandomController extends Controller
327329
{
328-
public function indexAction($name)
330+
public function indexAction($limit)
329331
{
332+
$number = rand(1, $limit);
333+
330334
return $this->render(
331-
'AcmeHelloBundle:Hello:index.html.twig',
332-
array('name' => $name)
335+
'AcmeWebsiteBundle:Random:index.html.twig',
336+
array('number' => $number)
333337
);
334338
335339
// render a PHP template instead
336340
// return $this->render(
337-
// 'AcmeHelloBundle:Hello:index.html.php',
338-
// array('name' => $name)
341+
// 'AcmeWebsiteBundle:Random:index.html.php',
342+
// array('number' => $number)
339343
// );
340344
}
341345
}
@@ -358,7 +362,7 @@ By default, Symfony2 supports two different templating languages: classic
358362
PHP templates and the succinct but powerful `Twig`_ templates. Don't be
359363
alarmed - you're free to choose either or even both in the same project.
360364

361-
The controller renders the ``AcmeHelloBundle:Hello:index.html.twig`` template,
365+
The controller renders the ``AcmeWebsiteBundle:Random:index.html.twig`` template,
362366
which uses the following naming convention:
363367

364368
**BundleName**:**ControllerName**:**TemplateName**
@@ -368,27 +372,27 @@ location using the following convention.
368372

369373
**/path/to/BundleName**/Resources/views/**ControllerName**/**TemplateName**
370374

371-
In this case, ``AcmeHelloBundle`` is the bundle name, ``Hello`` is the
375+
In this case, ``AcmeWebsiteBundle`` is the bundle name, ``Random`` is the
372376
controller, and ``index.html.twig`` the template:
373377

374378
.. configuration-block::
375379

376380
.. code-block:: jinja
377381
:linenos:
378382
379-
{# src/Acme/HelloBundle/Resources/views/Hello/index.html.twig #}
383+
{# src/Acme/WebsiteBundle/Resources/views/Random/index.html.twig #}
380384
{% extends '::base.html.twig' %}
381385
382386
{% block body %}
383-
Hello {{ name }}!
387+
Number: {{ number }}
384388
{% endblock %}
385389
386390
.. code-block:: html+php
387391

388-
<!-- src/Acme/HelloBundle/Resources/views/Hello/index.html.php -->
392+
<!-- src/Acme/WebsiteBundle/Resources/views/Random/index.html.php -->
389393
<?php $view->extend('::base.html.php') ?>
390394

391-
Hello <?php echo $view->escape($name) ?>!
395+
Number: <?php echo $view->escape($number) ?>
392396

393397
Step through the Twig template line-by-line:
394398

@@ -414,7 +418,7 @@ and in the ``app`` directory:
414418
<html>
415419
<head>
416420
<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>
418422
{% block stylesheets %}{% endblock %}
419423
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
420424
</head>
@@ -431,7 +435,7 @@ and in the ``app`` directory:
431435
<html>
432436
<head>
433437
<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>
435439
<?php $view['slots']->output('stylesheets') ?>
436440
<link rel="shortcut icon" href="<?php echo $view['assets']->getUrl('favicon.ico') ?>" />
437441
</head>
@@ -509,16 +513,16 @@ use a Kernel class, ``AppKernel``, to bootstrap the application.
509513

510514
.. code-block:: text
511515
512-
http://localhost/app.php/hello/Ryan
516+
http://localhost/app.php/random/10
513517
514518
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.
516520
By using Apache ``mod_rewrite`` rules, you can force the ``app.php`` file
517521
to be executed without needing to specify it in the URL:
518522

519523
.. code-block:: text
520524
521-
http://localhost/hello/Ryan
525+
http://localhost/random/10
522526
523527
Though front controllers are essential in handling every request, you'll
524528
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.
570574
.. code-block:: text
571575
572576
Class Name:
573-
Acme\HelloBundle\Controller\HelloController
577+
Acme\WebsiteBundle\Controller\RandomController
574578
Path:
575-
src/Acme/HelloBundle/Controller/HelloController.php
579+
src/Acme/WebsiteBundle/Controller/RandomController.php
576580
577581
The Source (``src``) Directory
578582
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -722,10 +726,10 @@ Bundle Directory Structure
722726

723727
The directory structure of a bundle is simple and flexible. By default, the
724728
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
726730
some of the most common elements of a bundle:
727731

728-
* ``Controller/`` contains the controllers of the bundle (e.g. ``HelloController.php``);
732+
* ``Controller/`` contains the controllers of the bundle (e.g. ``RandomController.php``);
729733

730734
* ``DependencyInjection/`` holds certain dependency injection extension classes,
731735
which may import service configuration, register compiler passes or more
@@ -907,14 +911,14 @@ the application via the development front controller:
907911

908912
.. code-block:: text
909913
910-
http://localhost/app_dev.php/hello/Ryan
914+
http://localhost/app_dev.php/random/10
911915
912916
If you'd like to see how your application will behave in the production environment,
913917
call the ``prod`` front controller instead:
914918

915919
.. code-block:: text
916920
917-
http://localhost/app.php/hello/Ryan
921+
http://localhost/app.php/random/10
918922
919923
Since the ``prod`` environment is optimized for speed; the configuration,
920924
routing and Twig templates are compiled into flat PHP classes and cached.

0 commit comments

Comments
 (0)