@@ -37,9 +37,121 @@ file::
37
37
38
38
.. tip ::
39
39
40
- In a default Symfony application that uses :doc: `Symfony Flex </setup/flex >`,
41
- bundles are enabled/disabled automatically for you when installing/removing
42
- them, so you don't need to look at or edit this ``bundles.php `` file.
40
+ In a default Symfony application that uses :doc: `Symfony Flex </setup/flex >`,
41
+ bundles are enabled/disabled automatically for you when installing/removing
42
+ them, so you don't need to look at or edit this ``bundles.php `` file.
43
+
44
+ Creating a Bundle
45
+ -----------------
46
+
47
+ The Symfony Standard Edition comes with a handy task that creates a fully-functional
48
+ bundle for you. Of course, creating a bundle by hand is pretty easy as well.
49
+
50
+ To show you how simple the bundle system is, create a new bundle called
51
+ AcmeTestBundle and enable it.
52
+
53
+ .. tip ::
54
+
55
+ The ``Acme `` portion is just a dummy name that should be replaced by
56
+ some "vendor" name that represents you or your organization (e.g.
57
+ ABCTestBundle for some company named ``ABC ``).
58
+
59
+ Start by creating a ``src/Acme/TestBundle/ `` directory and adding a new file
60
+ called ``AcmeTestBundle.php ``::
61
+
62
+ // src/Acme/TestBundle/AcmeTestBundle.php
63
+ namespace Acme\TestBundle;
64
+
65
+ use Symfony\Component\HttpKernel\Bundle\Bundle;
66
+
67
+ class AcmeTestBundle extends Bundle
68
+ {
69
+ }
70
+
71
+ .. tip ::
72
+
73
+ The name AcmeTestBundle follows the standard
74
+ :ref: `Bundle naming conventions <bundles-naming-conventions >`. You could
75
+ also choose to shorten the name of the bundle to simply TestBundle by naming
76
+ this class TestBundle (and naming the file ``TestBundle.php ``).
77
+
78
+ This empty class is the only piece you need to create the new bundle. Though
79
+ commonly empty, this class is powerful and can be used to customize the behavior
80
+ of the bundle.
81
+
82
+ Now that you've created the bundle, enable it via the ``AppKernel `` class::
83
+
84
+ // app/AppKernel.php
85
+ public function registerBundles()
86
+ {
87
+ $bundles = array(
88
+ // ...
89
+
90
+ // register your bundle
91
+ new Acme\TestBundle\AcmeTestBundle(),
92
+ );
93
+ // ...
94
+
95
+ return $bundles;
96
+ }
97
+
98
+ And while it doesn't do anything yet, AcmeTestBundle is now ready to be used.
99
+
100
+ And as easy as this is, Symfony also provides a command-line interface for
101
+ generating a basic bundle skeleton:
102
+
103
+ .. code-block :: terminal
104
+
105
+ $ php bin/console generate:bundle --namespace=Acme/TestBundle
106
+
107
+ The bundle skeleton generates a basic controller, template and routing
108
+ resource that can be customized. You'll learn more about Symfony's command-line
109
+ tools later.
110
+
111
+ .. tip ::
112
+
113
+ Whenever creating a new bundle or using a third-party bundle, always make
114
+ sure the bundle has been enabled in ``registerBundles() ``. When using
115
+ the ``generate:bundle `` command, this is done for you.
116
+
117
+ Bundle Directory Structure
118
+ --------------------------
119
+
120
+ The directory structure of a bundle is simple and flexible. By default, the
121
+ bundle system follows a set of conventions that help to keep code consistent
122
+ between all Symfony bundles. Take a look at AcmeDemoBundle, as it contains some
123
+ of the most common elements of a bundle:
124
+
125
+ ``Controller/ ``
126
+ Contains the controllers of the bundle (e.g. ``RandomController.php ``).
127
+
128
+ ``DependencyInjection/ ``
129
+ Holds certain Dependency Injection Extension classes, which may import service
130
+ configuration, register compiler passes or more (this directory is not
131
+ necessary).
132
+
133
+ ``Resources/config/ ``
134
+ Houses configuration, including routing configuration (e.g. ``routing.yml ``).
135
+
136
+ ``Resources/views/ ``
137
+ Holds templates organized by controller name (e.g. ``Random/index.html.twig ``).
138
+
139
+ ``Resources/public/ ``
140
+ Contains web assets (images, stylesheets, etc) and is copied or symbolically
141
+ linked into the project ``web/ `` directory via the ``assets:install `` console
142
+ command.
143
+
144
+ ``Tests/ ``
145
+ Holds all tests for the bundle.
146
+
147
+ A bundle can be as small or large as the feature it implements. It contains
148
+ only the files you need and nothing else.
149
+
150
+ As you move through the guides, you'll learn how to persist objects to a
151
+ database, create and validate forms, create translations for your application,
152
+ write tests and much more. Each of these has their own place and role within
153
+ the bundle.
154
+ >>>>>>> 3.4
43
155
44
156
Learn more
45
157
----------
0 commit comments