Porting a Symfony 3 application to Flex
Symfony Flex is a new project structure for Symfony Framework projects. It is not due until later this year. Even though Flex is not complete, yet you can already work with it and in my experience it is quite stable for such a raw product. This is likely because I'm using the stable Symfony 3.3 components with Symfony Flex.
The basic steps to install Symfony Flex are already well documented in the article on Symfony4 demo, so I won't cover that in detail. My experience builds on porting a simple Symfony3 Standard Edition to the current (June 2017) version of Symfony Flex.
Symfony Flex changes the project directory structure quite considerably, but to a developer versed in Symfony development it is immediately familiar. Some files are exactly where they used to be, and the configuration is still in YAML format.
The biggest change in Symfony Flex vs. the Symfony3 standard edition is that you get a lot less components by default. This now makes Flex more aking to the Silex microframework by default as you'll need to opt-in to a number of things that used to ship by default.
Moving from Symfony 3 to Flex
In my case the application was rather simple and all built in the AppBundle, which is best-practise in Symfony3 for many applications. I mostly had to move files and configurations around and change namespaces.
Here is a list that might not be comprehensive, but outlines the high level steps that I did to move from the application:
- Install Symfony Flex
- Move environment specific configuration from parameters.yml to .env
- Copy controllers, etc. code from Symfony3 dir ./src/AppBundle to ./src in your Flex installation
- Change namespaces in your classes from AppBundle/Xyz to App/Xyz
- Move static assets from ./src/AppBundle/Resources/public to ./web/assets and fix asset paths
- Add base configuration for your app into appropriate locations (routing to ./etc/routing.yaml, etc.)
- Start testing your application and add missing configurations, packages and recepies as you run into missing dependencies (templating, console, ORM...) and other issues
- Deploy ;)
For more details, you are welcome to check out the source code for both the original and the Symfony Flex app:
- Symfony3 app: https://github.com/janit/symfony-hybrid-twig-react-vue
- Symfony Flex app: https://github.com/janit/symfony-hybrid-flex-port
Symfony Flex performance improvement
Symfony Flex builds on the Microkernel introduced in Symfony 2.8. This and the smaller number of depencencies should translate into somewhat improved performance, with minimal changes (the ones above) into the codebase.
After upgrading the original prototype app to Symfony 3.3 I decided to do a quick round of benchmarks to compare the effect of the Symfony Port. The benchmarks were ran from two VPS servers, both with 1 CPU core and 1 GB RAM.
PHP version on the app server was 7.1 with OPcache and MySQL version is 5.7, instead. There are no special caches on the machine. Benchmarks were ran three times and the average of the runs were used.
The benchmarks were performed to the frontpage, which does some fetches from the database, some JSON encoding and template processing. So a rather typical workload for applications.
Here are the results:
I wouldn't get too excited without more indepth analysis, but it looks like you get on average 20% improved throughput with the Microkernel and other improvements in Symfony Flex.
It's not surprising that I ran into some bugs when installing recepies, etc. since Symfony Flex is far from ready. But for the core functionality I faced little issues and overall was happy with the new structure. I feel Symfony Flex will give little reason for myself to use the Silex microframework in the future.
In addition to the new structure, the apparent improvement in performance is obviously welcome. This would likely be even more evident where I could leave more dependencies out, for example in API workloads. This obviously won't magically push Symfony/PHP into Golang or Node.js territory for raw API throughput, but for existing large code bases it could provide a low-effort boost.
Overall Symfony Flex is a welcome breath of fresh air to the Symfony community!
- Symfony Flex set to enable RAD (Rapid Application Development)
- PHP microframeworks and the Symfony Framework Micro Kernel
- Symfony Benchmarks: Symfony Microkernel, Lumen, Silex, Slim...
- Running Symfony Applications with PHP-PM or PHPFastCGI