Categories: All Symfony Tutorials, Tips And Guides you needLearn PHP Tutorials, Tips And Guides

Using Symfony Flex On Cloudways for Symfony Application Management

Reading Time: 5 minutes

PHP has maintained its lead by introducing new features that try to keep up with the emerging trends in the dev world. In particular, frameworks such as Symfony ensures that developers have access to newest and latest tools for RAD (Rapid Application Development).

This is why many managed PHP MySQL hosting solution providers including Cloudways have made PHP 7.1 available on their platforms. Since Cloudways supports Symfony 3.3, I decided to try out the Symfony Flex, the latest (proposed) way of managing Symfony applications. As per Symfony docs:

Flex is not a new Symfony version, but a tool that replaces and improves the Symfony Installer. Symfony Flex automates the most common tasks of Symfony applications, such as installing and removing bundles and other dependencies. Symfony Flex works for Symfony 3.3 and newer versions.

Internally, Symfony Flex is a Composer plugin that modifies the behavior of the require and update commands. When installing or updating dependencies in a Flex-enabled application, Symfony can perform tasks before and after the execution of Composer tasks.

In this initial overview of Symfony Flex, I will demonstrate the installation of Symfony Flex and management of a Symfony application on Cloudways managed PHP hosting solution. For the purpose of this article, I am assuming that you have already signed up at Cloudways and have already launched a PHP app. You can follow the following GIF for more information:

Install Symfony Flex

Symfony already provides a skeleton project that is ideal for installing a new Symfony Flex enabled application. This is a single composer.json file which automatically run `composer install` command. During the execution of the command, Symfony/flex is the first package that is installed. After that, Flex hooks into Composer processes. Fabien explained the workflow of Flex as:

When Composer installs (or updates) other dependencies, Symfony Flex looks for an associated recipe on the Symfony Flex server, and executes it

Now to install Flex enabled Symfony app on Cloudways, open SSH Terminal and go to your application folder using the `cd` command and run the following command:

composer create-project symfony/skeleton api

This command will install the skeleton project consist symfony of 3.3.

Notice that some commands (such as cache and symlink) are running automatically near the end of the process. This is because of a MAKEFILE which automates the process to some degree.

You might also like: Why Choose Symfony For Your Next Project?

The Directory Structure

Symfony 4.0 will have a new and better simplified directory structure which will let the developers compose applications structure. Symfony Flex is also based on this directory structure. Here is how this amazingly simple structure looks like:

Notice the config folder which has bundles.php. Flex will register all the bundles automatically after installation. The public folder offers access to the application through the index.php file, while the src folder contains the controllers and Entities.

An API Project Using Symfony Flex

So far I have installed Flex and discussed the new Symfony 4. directory structure. As you know Fabien created an API project to demonstrate the power of Flex. I will now attempt to replicate the project on Cloudways. The good thing is that Flex knows about the aliases to install the respective Symfony bundles. It automatically locate the recipes, even if a bundle does not contain a recipe symfony detects Composer packages with the symfony-bundle type and automatically enables them for all environments.

You might also like: How To Create Simple REST API In Symfony 3.1

Let’s install the API project using the following command:

composer req api      //where api is the alias of api-platform/api-pack

The bundle is successfully installed and doesn’t need any further configurations (Symfony will take care of it all). At this time,if you see the directory structure, you will notice that several folders ( bin, templates, translations and var) have been added. I will use bin/console for working with console commands. You can also install symfony console separately with `composer req cli` command.

Configure Database Credentials in .env File

Next, I will add database credentials to .env file (located in the root of the project). This file is globally available throughout the project and you can add further environment variables to it.

Note: DB credentials are available at the Application Access Details page.

The following is the addition to the .env file:

###> symfony/framework-bundle ###



###< doctrine/doctrine-bundle ###

Create A Doctrine Entity  

I will now create a Doctrine entity hello.php (placed in src/Entity folder). Add the following basic code to the file:


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

* @ApiResource
* @ORM\Entity

class Hello // The class name will be used to name exposed resources

    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")

   public $id;

    * @param string $name A name property - this description will be available in the API documentation too.
    * @ORM\Column
    * @Assert\NotBlank

   public $name;


Symfony will use the namespace `App` for requiring the source files. As you could see, I’ve simply added the name and ID to the class. Now run the update schema command to generate the table.

Remember that at this point, you don’t need to install an internal web server as Cloudways takes care of it. However, if you are working on a local machine, you can install the web server through the `composer req server`. Additionally, I will update the webroot of the application and add api/public to it.

Now in the Access Details Tab, click the application URL.

You should see your API application in action. In my case the URL is: http://phpstack-71265-297440.cloudwaysapps.com/

Wrapping it up!

As you could see from the above sample project, Symfony Flex has removed much of the complexity of Symfony application management. I strongly recommend that php developers should try it out for themselves so that they could understand how this great tool works for their applications. If you need help in trying out Symfony Flex, do leave a comment.

Shahroze Nawaz :Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com