|
| 1 | +Creating the Project |
| 2 | +==================== |
| 3 | + |
| 4 | +Installing Symfony |
| 5 | +------------------ |
| 6 | + |
| 7 | +There is only one recommended way to install Symfony: |
| 8 | + |
| 9 | +.. best-practice:: |
| 10 | + |
| 11 | + Always use `Composer`_ to install Symfony. |
| 12 | + |
| 13 | +Composer is the dependency manager used by modern PHP applications. Adding or |
| 14 | +removing requirements for your project and updating the third-party libraries |
| 15 | +used by your code is a breeze thanks to Composer. |
| 16 | + |
| 17 | +Dependency Management with Composer |
| 18 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | + |
| 20 | +Before installing Symfony, you need to make sure that you have Composer installed |
| 21 | +globally. Open your terminal (also called *command console*) and run the following |
| 22 | +command: |
| 23 | + |
| 24 | +.. code-block:: bash |
| 25 | +
|
| 26 | + $ composer --version |
| 27 | + Composer version 1e27ff5e22df81e3cd0cd36e5fdd4a3c5a031f4a 2014-08-11 15:46:48 |
| 28 | +
|
| 29 | +You'll probably see a different version identifier. Never mind because Composer |
| 30 | +is updated on a continuous basis and its specific version doesn't matter. |
| 31 | + |
| 32 | +Installing Composer Globally |
| 33 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 34 | + |
| 35 | +In case you don't have Composer installed globally, execute the following two |
| 36 | +commands if you use Linux or Mac OS X (the second command will ask for your |
| 37 | +user password): |
| 38 | + |
| 39 | +.. code-block:: bash |
| 40 | +
|
| 41 | + $ curl -sS https://getcomposer.org/installer | php |
| 42 | + $ sudo mv composer.phar /usr/local/bin/composer |
| 43 | +
|
| 44 | +.. note:: |
| 45 | + |
| 46 | + Depending on your Linux distribution, you may need to execute ``su`` command |
| 47 | + instead of ``sudo``. |
| 48 | + |
| 49 | +If you use a Windows system, download the executable installer from the |
| 50 | +`Composer download page`_ and follow the steps to install it. |
| 51 | + |
| 52 | +Creating the Blog Application |
| 53 | +----------------------------- |
| 54 | + |
| 55 | +Now that everything is correctly set up, you can create a new project based on |
| 56 | +Symfony. In your command console, browse to a directory where you have permission |
| 57 | +to create files and execute the following commands: |
| 58 | + |
| 59 | +.. code-block:: bash |
| 60 | +
|
| 61 | + $ cd projects/ |
| 62 | + $ composer create-project symfony/framework-standard-edition blog/ |
| 63 | +
|
| 64 | +This command will create a new directory called ``blog`` that will contain |
| 65 | +a fresh new project based on the most recent stable Symfony version available. |
| 66 | + |
| 67 | +Checking the Symfony Installation |
| 68 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 69 | + |
| 70 | +Once the installation is finished, enter the ``blog/`` directory and check that |
| 71 | +Symfony is correctly installed by executing the following command: |
| 72 | + |
| 73 | +.. code-block:: bash |
| 74 | +
|
| 75 | + $ cd blog/ |
| 76 | + $ php app/console --version |
| 77 | +
|
| 78 | + Symfony version 2.6.* - app/dev/debug |
| 79 | +
|
| 80 | +If you see the installed Symfony version, everything worked as expected. If not, |
| 81 | +you can execute the following *script* to check what does prevent your system |
| 82 | +from correctly executing Symfony applications: |
| 83 | + |
| 84 | +.. code-block:: bash |
| 85 | +
|
| 86 | + $ php app/check.php |
| 87 | +
|
| 88 | +Depending on your system, you can see up to two different lists when executing the |
| 89 | +`check.php` script. The first one shows the mandatory requirements which your |
| 90 | +system must meet to execute Symfony applications. The second list shows the |
| 91 | +optional requirements suggested for an optimal execution of Symfony applications: |
| 92 | + |
| 93 | +.. code-block:: bash |
| 94 | +
|
| 95 | + Symfony2 Requirements Checker |
| 96 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 97 | +
|
| 98 | + > PHP is using the following php.ini file: |
| 99 | + /usr/local/zend/etc/php.ini |
| 100 | +
|
| 101 | + > Checking Symfony requirements: |
| 102 | + .....E.........................W..... |
| 103 | +
|
| 104 | + [ERROR] |
| 105 | + Your system is not ready to run Symfony2 projects |
| 106 | +
|
| 107 | + Fix the following mandatory requirements |
| 108 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 109 | +
|
| 110 | + * date.timezone setting must be set |
| 111 | + > Set the "date.timezone" setting in php.ini* (like Europe/Paris). |
| 112 | +
|
| 113 | + Optional recommendations to improve your setup |
| 114 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 115 | +
|
| 116 | + * short_open_tag should be disabled in php.ini |
| 117 | + > Set short_open_tag to off in php.ini*. |
| 118 | +
|
| 119 | +
|
| 120 | +.. tip:: |
| 121 | + |
| 122 | + Symfony releases are digitally signed for security reasons. If you want to |
| 123 | + verify the integrity of your Symfony installation, take a look at the |
| 124 | + `public checksums repository`_ and follow `these steps`_ to verify the |
| 125 | + signatures. |
| 126 | + |
| 127 | +Structuring the Application |
| 128 | +--------------------------- |
| 129 | + |
| 130 | +After creating the application, enter the ``blog/`` directory and you'll see a |
| 131 | +number of files and directories generated automatically: |
| 132 | + |
| 133 | +.. code-block:: text |
| 134 | +
|
| 135 | + blog/ |
| 136 | + ├─ app/ |
| 137 | + │ ├─ console |
| 138 | + │ ├─ cache/ |
| 139 | + │ ├─ config/ |
| 140 | + │ ├─ logs/ |
| 141 | + │ └─ Resources/ |
| 142 | + ├─ src/ |
| 143 | + │ └─ AppBundle/ |
| 144 | + ├─ vendor/ |
| 145 | + └─ web/ |
| 146 | +
|
| 147 | +This file and directory hierarchy is the convention proposed by Symfony to |
| 148 | +structure your applications. The recommended purpose of each directory is the |
| 149 | +following: |
| 150 | + |
| 151 | +* ``app/cache/``, stores all the cache files generated by the application; |
| 152 | +* ``app/config/``, stores all the configuration defined for any environment; |
| 153 | +* ``app/logs/``, stores all the log files generated by the application; |
| 154 | +* ``app/Resources/``, stores all the templates and the translation files for the |
| 155 | + application; |
| 156 | +* ``src/AppBundle/``, stores the Symfony specific code (controllers and routes), |
| 157 | + your domain code (e.g. Doctrine classes) and all your business logic; |
| 158 | +* ``vendor/``, this is the directory where Composer installs the application's |
| 159 | + dependencies and you should never modify any of its contents; |
| 160 | +* ``web/``, stores all the front controller files and all the web assets, such |
| 161 | + as stylesheets, JavaScript files and images. |
| 162 | + |
| 163 | +Application Bundles |
| 164 | +~~~~~~~~~~~~~~~~~~~ |
| 165 | + |
| 166 | +When Symfony 2.0 was released, most developers naturally adopted the symfony |
| 167 | +1.x way of dividing applications into logical modules. That's why many Symfony |
| 168 | +apps use bundles to divide their code into logical features: ``UserBundle``, |
| 169 | +``ProductBundle``, ``InvoiceBundle``, etc. |
| 170 | + |
| 171 | +But a bundle is *meant* to be something that can be reused as a stand-alone |
| 172 | +piece of software. If ``UserBundle`` cannot be used *"as is"* in other Symfony |
| 173 | +apps, then it shouldn't be its own bundle. Moreover ``InvoiceBundle`` depends |
| 174 | +on ``ProductBundle``, then there's no advantage to having two separate bundles. |
| 175 | + |
| 176 | +.. best-practice:: |
| 177 | + |
| 178 | + Create only one bundle called ``AppBundle`` for your application logic |
| 179 | + |
| 180 | +Implementing a single ``AppBundle`` bundle in your projects will make your code |
| 181 | +more concise and easier to understand. Starting in Symfony 2.6, the official |
| 182 | +Symfony documentation uses the ``AppBundle`` name. |
| 183 | + |
| 184 | +.. note:: |
| 185 | + |
| 186 | + There is no need to prefix the ``AppBundle`` with your own vendor (e.g. |
| 187 | + ``AcmeAppBundle``), because this application bundle is never going to be |
| 188 | + shared. |
| 189 | + |
| 190 | +All in all, this is the typical directory structure of a Symfony application |
| 191 | +that follows these best practices: |
| 192 | + |
| 193 | +.. code-block:: text |
| 194 | +
|
| 195 | + blog/ |
| 196 | + ├─ app/ |
| 197 | + │ ├─ console |
| 198 | + │ ├─ cache/ |
| 199 | + │ ├─ config/ |
| 200 | + │ ├─ logs/ |
| 201 | + │ └─ Resources/ |
| 202 | + ├─ src/ |
| 203 | + │ └─ AppBundle/ |
| 204 | + ├─ vendor/ |
| 205 | + └─ web/ |
| 206 | + ├─ app.php |
| 207 | + └─ app_dev.php |
| 208 | +
|
| 209 | +.. tip:: |
| 210 | + |
| 211 | + If you are using Symfony 2.6 or a newer version, the ``AppBundle`` bundle |
| 212 | + is already generated for you. If you are using an older Symfony version, |
| 213 | + you can generate it by hand executing this command: |
| 214 | + |
| 215 | + .. code-block:: bash |
| 216 | +
|
| 217 | + $ php app/console generate:bundle --namespace=AppBundle --dir=src --format=annotation --no-interaction |
| 218 | +
|
| 219 | +Extending the Directory Structure |
| 220 | +--------------------------------- |
| 221 | + |
| 222 | +If your project or infrastructure requires some changes to the default directory |
| 223 | +structure of Symfony, you can `override the location of the main directories`_: |
| 224 | +``cache/``, ``logs/`` and ``web/``. |
| 225 | + |
| 226 | +In addition, Symfony3 will use a slightly different directory structure when |
| 227 | +it's released: |
| 228 | + |
| 229 | +.. code-block:: text |
| 230 | +
|
| 231 | + blog-symfony3/ |
| 232 | + ├─ app/ |
| 233 | + │ ├─ config/ |
| 234 | + │ └─ Resources/ |
| 235 | + ├─ bin/ |
| 236 | + │ └─ console |
| 237 | + ├─ src/ |
| 238 | + ├─ var/ |
| 239 | + │ ├─ cache/ |
| 240 | + │ └─ logs/ |
| 241 | + ├─ vendor/ |
| 242 | + └─ web/ |
| 243 | +
|
| 244 | +The changes are pretty superficial, but for now, we recommend that you use |
| 245 | +the Symfony2 directory structure. |
| 246 | + |
| 247 | +.. _`Composer`: https://getcomposer.org/ |
| 248 | +.. _`Get Started`: https://getcomposer.org/doc/00-intro.md |
| 249 | +.. _`Composer download page`: https://getcomposer.org/download/ |
| 250 | +.. _`override the location of the main directories`: http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html |
| 251 | +.. _`public checksums repository`: https://github.com/sensiolabs/checksums |
| 252 | +.. _`these steps`: http://fabien.potencier.org/article/73/signing-project-releases |
| 253 | + |
0 commit comments