A few months ago, the alpha version of Composer
Symfony Flex was released . With the release of Symfony 3.3, it became possible to test the work of this plugin and to “taste it” an approach to building applications on Symfony 4. What we are trying to do now.
In the
previous post I wrote about the structure of the Symfony 4 application and some features, so now I will not focus on this. In order to make the installation process smooth, I recommend Windows users to use GitBash or Cygwin terminal. In fact, you can use any terminal, the main thing is to have the make utility available and the linux-tools installed. You also need PHP version ^ 7.1.3
Symfony flex
A few words about the plugin itself. Symfony Flex takes care of setting up the components installed in your application using Composer. These do not have to be symfony components. Plugin works with recipes. If the recipe for the package is defined in the repository, then Symfony Flex will do its job, otherwise you will have to configure everything by hand as before.
At the moment there are two repository of recipes, the
main (or official) and
public . The main repository is different in that it can define aliases for packages and the placement of the recipe in this repository must be approved by core-team Symfony. In a public repository, softer rules for posting.
')
Plug-in installation
First, install symfony Flex:
composer require symfony/flex
From this point on, the plugin controls the installation of packages in your application. To get a simple “Hello World” application, install symfony / framework-bundle. We can already use the “magic” of the Symfony Flex plugin and use a convenient alias for this:
composer req frameworkbundle
Now let's look at the directory structure:
/etc /src /vendor /web .env .env.dist .gitignore composer.json composer.lock Makefile
The / etc directory now contains the entire configuration and the bundles.php file, which contains an array of all bundles installed in your project. This file is modified by the symfony Flex plugin each time you install or uninstall the bundle. If you want to use a local bundle, you will have to manually register it into this file.
Container file is provided for container configuration. Yaml, for routing routing.yaml. Also in / etc you will see the packages directory, which stores the minimum default configurations for all components. In essence, these configurations are copied from recipes.
In the / src directory, the Kernel class is located, redesigned for a new directory structure. There is also an empty Controller directory.
The folder / web is now only index.php for all types of environments. If you look at the contents of this file, you will notice that the symfony / dotenv component is used to simulate the presence of environment variables. This component ships by default with symfony 3.3. In development mode, it allows defining environment variables in the .env file.
Let's install this component:
composer req symfony/dotenv
Theoretically, we set the minimum set of components required to run the application. But there is one nuance. By default, recipes are shipped with a configuration in YAML. And so even if you want to write your own configurations in a different format, you still need to install the symfony / yaml component so that the configurations of all components can be loaded. Otherwise, you will have to rewrite all configs in the / etc / packages directory to the format you need each time you add a new component.
Let's install symfony / yaml:
composer req symfony/yaml
Almost everything is ready. It remains a little "conjuring" over the file composer.json. The first thing you need to do is to register the psr-4 section, for autoloading our classes. Since all the code we have now is right in / src, we add:
{ "autoload": { "psr-4": { "App\\": "src/" } } }
Note the following section in composer.json:
{ "scripts": { "auto-scripts": { "make cache-warmup": "script", "assets:install --symlink --relative %WEB_DIR%": "symfony-cmd" } } }
In this section, the symfony Flex plugin adds the necessary command calls for each component. These challenges are spelled out in recipes. With the current configuration, the section “auto-scripts” will not be executed by the plugin. In order to fix this you need to add the following parameters:
"post-install-cmd": [ "@auto-scripts" ], "post-update-cmd": [ "@auto-scripts" ]
This template allows the symfony Flex plugin to join the process when the post-install and post-update events of the composer occur.
Final view composer.json { "require": { "symfony/flex": "^1.0", "symfony/framework-bundle": "^3.3", "symfony/dotenv": "^3.3", "symfony/yaml": "^3.3" }, "autoload": { "psr-4": { "App\\": "src/" } }, "scripts": { "auto-scripts": { "make cache-warmup": "script", "assets:install --symlink --relative %WEB_DIR%": "symfony-cmd" }, "post-install-cmd": [ "@auto-scripts" ], "post-update-cmd": [ "@auto-scripts" ] } }
Now let Composer rebuild the autoload class:
composer dump-autoload
It's time to add a controller. Create the TestController class in the / src / Controller folder:
namespace App\Controller; use Symfony\Component\HttpFoundation\Response; class TestController { public function test() { return new Response('It works!'); } }
Now you need to register the route in the /etc/routing.yaml file:
index: path: / defaults: {_controller: 'App\Controller\TestController::test'}
We start the web server with make:
make serve
Go to the browser at 127.0.0.1:8000 and see our message “It works!”.
Minimal application is built and running, but I want to use annotations to determine routes. No problem! For this we need sensio / framework-extra-bundle and symfony / annotations-pack, for the latter, a convenient annot alias is provided.
composer req sensio/framework-extra-bundle composer req annot
Now we will comment out the route in the file routing.yaml, which we defined before, and instead of it, we indicate where our controller classes for the annotation parser are:
controllers: resource: ../src/Controller/ type: annotation
Let's change our controller by adding annotation for the route (don't forget to add use Sensio \ Bundle \ FrameworkExtraBundle \ Configuration \ Route as well):
namespace App\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; class TestController { public function test() { return new Response('It works!'); } }
Go to 127.0.0.1:8000/test and see our message. If we no longer need to use annotations, then we can remove the components:
composer rem annot composer rem sensio/framework-extra-bundle
All that remains is to return the routing settings to routing.yaml, and everything will work. The symfony Flex plugin will take care to remove all the garbage, as well as remove the bundle sensio / framework-extra-bundle from the configuration.
This is the way to experiment with symfony 4 style apps right now.
To tell the truth, you can use the ready-made composer.json file to install such an application out of the box:
composer create-project "symfony/skeleton:^3.3" demo
I described the manual assembly process of the project in order to focus on some details.
Project sources can be found
here .
Thanks for attention.