@@ -34,15 +34,64 @@ to the following URL:
3434
3535 http://localhost/app_dev.php/hello/Symfony
3636
37+ In reality, you'll be able to replace ``Symfony `` with any other name to be
38+ greeted. To create the page, we'll go through the simple two-step process.
39+
3740.. note ::
3841
3942 The tutorial assumes that you've already downloaded Symfony2 and configured
4043 your webserver. The above URL assumes that ``localhost `` points to the
4144 ``web `` directory of your new Symfony2 project. For detailed information
4245 on this process, see the :doc: `Installing Symfony2</book/installation> `.
46+
47+ If you've downloaded the `Symfony Standard Edition `_, delete the
48+ ``src/Acme/DemoBundle `` directory, as you'll recreate it in this chapter.
4349
44- In reality, you'll be able to replace ``Symfony `` with any other name to be
45- greeted. To create the page, we'll go through the simple two-step process.
50+ Create the Bundle
51+ ~~~~~~~~~~~~~~~~~
52+
53+ Before you begin, you'll need to create a *bundle *. In Symfony2, a bundle
54+ is like a plugin, except that all of the code in your application will live
55+ inside a bundle.
56+
57+ A bundle is nothing more than a directory (with a PHP namespace) that houses
58+ everything related to a specific feature (see :ref: `page-creation-bundles `).
59+ To create a bundle called ``AcmeDemoBundle ``, run the following command:
60+
61+ .. code-block :: text
62+
63+ php app/console init:bundle "Acme\DemoBundle" src
64+
65+ Next, be sure that the ``Acme `` namespace is loaded by adding the following
66+ to the ``app/autoload.php `` file (see the :ref: `Autoloading sidebar<autoloading-introduction-sidebar> `):
67+
68+ .. code-block :: php
69+
70+ $loader->registerNamespaces(array(
71+ 'Acme' => __DIR__.'/../src',
72+ // ...
73+ ));
74+
75+ Finally, initialize the bundle by adding it to the ``registerBundles `` method
76+ of the ``AppKernel `` class:
77+
78+ .. code-block :: php
79+
80+ // app/AppKernel.php
81+ public function registerBundles()
82+ {
83+ $bundles = array(
84+ // ...
85+ new Acme\DemoBundle\AcmeDemoBundle(),
86+ );
87+
88+ // ...
89+
90+ return $bundles;
91+ }
92+
93+ Now that you have a bundle setup, you can begin building your application
94+ inside the bundle.
4695
4796Create the Route
4897~~~~~~~~~~~~~~~~
@@ -61,7 +110,7 @@ you can also choose to use XML or PHP out of the box to configure routes:
61110 defaults : { _controller: FrameworkBundle:Default:index }
62111
63112 hello :
64- resource : @HelloBundle /Resources/config/routing.yml
113+ resource : @AcmeDemoBundle /Resources/config/routing.yml
65114
66115 .. code-block :: xml
67116
@@ -76,7 +125,7 @@ you can also choose to use XML or PHP out of the box to configure routes:
76125 <default key =" _controller" >FrameworkBundle:Default:index</default >
77126 </route >
78127
79- <import resource =" @HelloBundle /Resources/config/routing.xml" />
128+ <import resource =" @AcmeDemoBundle /Resources/config/routing.xml" />
80129 </routes >
81130
82131 .. code-block :: php
@@ -89,7 +138,7 @@ you can also choose to use XML or PHP out of the box to configure routes:
89138 $collection->add('homepage', new Route('/', array(
90139 '_controller' => 'FrameworkBundle:Default:index',
91140 )));
92- $collection->addCollection($loader->import("@HelloBundle /Resources/config/routing.php"));
141+ $collection->addCollection($loader->import("@AcmeDemoBundle /Resources/config/routing.php"));
93142
94143 return $collection;
95144
@@ -103,34 +152,34 @@ inside the ``HelloBundle``:
103152
104153 .. code-block :: yaml
105154
106- # src/Sensio/HelloBundle /Resources/config/routing.yml
155+ # src/Acme/DemoBundle /Resources/config/routing.yml
107156 hello :
108157 pattern : /hello/{name}
109- defaults : { _controller: HelloBundle :Hello:index }
158+ defaults : { _controller: AcmeDemoBundle :Hello:index }
110159
111160 .. code-block :: xml
112161
113- <!-- src/Sensio/HelloBundle /Resources/config/routing.xml -->
162+ <!-- src/Acme/DemoBundle /Resources/config/routing.xml -->
114163 <?xml version =" 1.0" encoding =" UTF-8" ?>
115164
116165 <routes xmlns =" http://symfony.com/schema/routing"
117166 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
118167 xsi : schemaLocation =" http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd" >
119168
120169 <route id =" hello" pattern =" /hello/{name}" >
121- <default key =" _controller" >HelloBundle :Hello:index</default >
170+ <default key =" _controller" >AcmeDemoBundle :Hello:index</default >
122171 </route >
123172 </routes >
124173
125174 .. code-block :: php
126175
127- // src/Sensio/HelloBundle /Resources/config/routing.php
176+ // src/Acme/DemoBundle /Resources/config/routing.php
128177 use Symfony\Component\Routing\RouteCollection;
129178 use Symfony\Component\Routing\Route;
130179
131180 $collection = new RouteCollection();
132181 $collection->add('hello', new Route('/hello/{name}', array(
133- '_controller' => 'HelloBundle :Hello:index',
182+ '_controller' => 'AcmeDemoBundle :Hello:index',
134183 )));
135184
136185 return $collection;
@@ -163,9 +212,9 @@ from the request to build and prepare the resource being requested. Except
163212in some advanced cases, the end product of a controller is always the same:
164213a Symfony2 ``Response `` object::
165214
166- // src/Sensio/HelloBundle /Controller/HelloController.php
215+ // src/Acme/DemoBundle /Controller/HelloController.php
167216
168- namespace Sensio\HelloBundle \Controller;
217+ namespace Acme\DemoBundle \Controller;
169218 use Symfony\Component\HttpFoundation\Response;
170219
171220 class HelloController
@@ -201,9 +250,9 @@ Templates allows us to move all of the presentation (e.g. HTML code) into
201250a separate file and reuse different portions of the page layout. Instead
202251of writing the HTML inside the controller, use a template instead::
203252
204- // src/Sensio/HelloBundle /Controller/HelloController.php
253+ // src/Acme/DemoBundle /Controller/HelloController.php
205254
206- namespace Sensio\HelloBundle \Controller;
255+ namespace Acme\DemoBundle \Controller;
207256
208257 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
209258
@@ -246,7 +295,7 @@ controller, and ``index.html.twig`` the template:
246295 .. code-block :: jinja
247296 :linenos:
248297
249- {# src/Sensio/HelloBundle /Resources/views/Hello/index.html.twig #}
298+ {# src/Acme/DemoBundle /Resources/views/Hello/index.html.twig #}
250299 {% extends '::layout.html.twig' %}
251300
252301 {% block body %}
@@ -255,8 +304,8 @@ controller, and ``index.html.twig`` the template:
255304
256305 .. code-block :: php
257306
258- <!-- src/Sensio/HelloBundle /Resources/views/Hello/index.html.php -->
259- <?php $view->extend('HelloBundle ::layout.html.php') ?>
307+ <!-- src/Acme/DemoBundle /Resources/views/Hello/index.html.php -->
308+ <?php $view->extend('AcmeDemoBundle ::layout.html.php') ?>
260309
261310 Hello <?php echo $view->escape($name) ?>!
262311
@@ -419,20 +468,21 @@ each of these directories in later chapters.
419468 behalf the instance you need a class::
420469
421470 $loader->registerNamespaces(array(
422- 'Sensio ' => __DIR__.'/../src',
471+ 'Acme ' => __DIR__.'/../src',
423472 // ...
424473 ));
425474
426475 With this configuration, Symfony2 will look inside the ``src `` directory
427- for any class in the ``Sensio `` namespace. For autoloading to work,
428- the class name and path to the file must follow the same pattern:
476+ for any class in the ``Acme `` namespace (your pretend company's namespace).
477+ For autoloading to work, the class name and path to the file must follow
478+ the same pattern:
429479
430480 .. code-block :: text
431481
432482 Class Name:
433- Sensio\HelloBundle \Controller\HelloController
483+ Acme\DemoBundle \Controller\HelloController
434484 Path:
435- src/Sensio/HelloBundle /Controller/HelloController.php
485+ src/Acme/DemoBundle /Controller/HelloController.php
436486
437487 The ``app/autoload.php `` configures the autoloader to look for different
438488 PHP namespaces in different directories and can be customized as necessary.
@@ -449,6 +499,8 @@ with *bundles* that contain your application code.
449499
450500But what exactly is a :term: `bundle `?
451501
502+ .. _page-creation-bundles :
503+
452504The Bundle System
453505-----------------
454506
@@ -491,7 +543,7 @@ method of the ``AppKernel`` class::
491543 //new Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle(),
492544
493545 // register your bundles
494- new Sensio\HelloBundle\HelloBundle (),
546+ new Acme\DemoBundle\AcmeDemoBundle (),
495547 );
496548
497549 if (in_array($this->getEnvironment(), array('dev', 'test'))) {
@@ -507,31 +559,31 @@ are used by your application (including the core Symfony bundles).
507559.. tip ::
508560
509561 A bundle can live *anywhere * as long as it can be autoloaded by Symfony2.
510- For example, if ``SensioHelloBundle `` lives inside the ``src/Sensio ``
511- directory, be sure that the ``Sensio `` namespace has been added to the
562+ For example, if ``AcmeDemoBundle `` lives inside the ``src/Acme ``
563+ directory, be sure that the ``Acme `` namespace has been added to the
512564 ``app/autoload.php `` file and mapped to the ``src `` directory.
513565
514566Creating a Bundle
515567~~~~~~~~~~~~~~~~~
516568
517569To show you how simple the bundle system is, let's create a new bundle called
518- ``SensioMyBundle `` and enable it.
570+ ``AcmeTestBundle `` and enable it.
519571
520- First, create a ``src/Sensio/MyBundle / `` directory and add a new file
521- called ``SensioMyBundle .php ``::
572+ First, create a ``src/Acme/TestBundle / `` directory and add a new file
573+ called ``AcmeTestBundle .php ``::
522574
523- // src/Sensio/MyBundle/SensioMyBundle .php
524- namespace Sensio\MyBundle ;
575+ // src/Acme/TestBundle/AcmeTestBundle .php
576+ namespace Acme\TestBundle ;
525577
526578 use Symfony\Component\HttpKernel\Bundle\Bundle;
527579
528- class SensioMyBundle extends Bundle
580+ class AcmeTestBundle extends Bundle
529581 {
530582 }
531583
532584.. tip ::
533585
534- The name ``SensioMyBundle `` follows the :ref: `Bundle naming conventions<bundles-naming-conventions> `.
586+ The name ``AcmeTestBundle `` follows the :ref: `Bundle naming conventions<bundles-naming-conventions> `.
535587
536588This empty class is the only piece we need to create our new bundle. Though
537589commonly empty, this class is powerful and can be used to customize the behavior
@@ -547,20 +599,21 @@ class::
547599 // ...
548600
549601 // register your bundles
550- new Sensio\MyBundle\SensioMyBundle (),
602+ new Acme\TestBundle\AcmeTestBundle (),
551603 );
552604
553605 // ...
554606
555607 return $bundles;
556608 }
557609
558- And while it doesn't do anything yet, ``MyBundle `` is now ready to be used.
610+ And while it doesn't do anything yet, ``AcmeTestBundle `` is now ready to
611+ be used.
559612
560613And as easy as this is, Symfony also provides a command-line interface for
561614generating a basic bundle skeleton::
562615
563- ./ app/console init:bundle "Sensio\MyBundle " src
616+ php app/console init:bundle "Acme\TestBundle " src
564617
565618The bundle skeleton generates with a basic controller, template and routing
566619resource that can be customized. We'll talk more about Symfony2's command-line
@@ -576,7 +629,7 @@ Bundle Directory Structure
576629
577630The directory structure of a bundle is simple and flexible. By default, the
578631bundle system follows a set of conventions that help to keep code consistent
579- between all Symfony2 bundles. Let's take a look at ``HelloBundle ``, as it
632+ between all Symfony2 bundles. Let's take a look at ``AcmeDemoBundle ``, as it
580633contains some of the most common elements of a bundle:
581634
582635* *Controller/ * contains the controllers of the bundle (e.g. ``HelloController.php ``);
@@ -884,3 +937,4 @@ Learn more from the Cookbook
884937
885938.. _`Twig` : http://www.twig-project.org
886939.. _`third-party bundles` : http://symfony2bundles.org/
940+ .. _`Symfony Standard Edition` : http://symfony.com/download
0 commit comments