According to the experts, Symfony 3.1 is the latest stable release. Version 3.2 is in the pipeline and new updates are being made available on almost daily basis. This is the latest in the series of Symfony 3.1 tutorials. I previously discussed the issue of asset management in Symfony 3.1.
In this blog, I am going to explain how you can create and manage App Bundles. I will discuss properties, directory structure, bundles creation, configuration and how to add third-party bundles.
Let’s start with what bundles are and how to create them in Symfony.
Understanding the Bundles System
Like other frameworks, Symfony has an excellent management system for app development. This system contains all the core functionalities, controllers and resources required for app development process. In essence, the bundles behave like extensions or plugins that you could work with independently.
Bundles optimize the way in which specific features could be enables or disabled. If you are following this series, the to-do app utilises a to-do bundle.
- When naming a bundle, Symfony allows alphanumeric characters and underscores only; for instance, Todo_App.
- Always use CamelCase
- Always use a well defined and easy-to-remember short name.
- Every bundle name should have the suffix “Bundle”.
Let’s start creating our first bundle step-by-step. If you need to install Symfony on your cloud server, follow this tutorial: How to install symfony 3.
Creating New Bundles:
Sign in to your Cloudways server with master credentials and open SSH terminal. In the terminal, move to the folder of the Symfony app. In my case it looks like this.
Now enter the command to generate bundles inside the project.
This command will take you through (very simple) steps.
First of all, it asks whether you wish to share the bundle in multiple applications. I will choose ‘no’ because I only need to create it inside the to-do app only.
The next step is to give a name to the bundle according to the convention. In this case, it will be FirstBundle. Remember that the suffix of the name should always be Bundle.
Now, just hit enter. Don’t put anything in the source because we want to create it in src/, which is the default path.
The final step is to select the annotation for configuration. I will leave the default as it is and hit enter.
The new bundle will be created inside src/ folder. If everything goes smoothly, you will see the OK message in the SSH terminal.
Now move to the project’s source folder and verify that the FirstBundle folder is there.
Directory Structure of the Newly Created Bundle
Let’s have a look at the directory structure of the bundle.
│ ├─ config/
│ ├─ doc/
│ │ └─ index.rst
│ ├─ translations/
│ ├─ views/
│ └─ public/
This is the standard structure for a bundle which contains all the compulsory files. FirstBundle.php converts plain directory into Symfony Bundle. Readme.md contains basic information of the bundle.
You can now start creating your code in the controller file located in the Controller folder. Similarly twig templates will be in the View folder under Resources.
How to Install Third-Party Bundles
As I said earlier, bundles are like plugins. Thus, you can install third-party bundles available in the standard plug-and-play form. A great source of these bundles is the Symfony packagist, friendsofsymfony. The process of installing these bundles is very simple:
Step 1: Install Via Composer
Installing a bundle using composer requires the following command.
Open the SSH terminal and enter the following command
$ composer require friendsofsymfony/user-bundle
user-bundle is the name of the bundle that you want.
This will install the best version of installation bundle available in the directory. However, this is not enough. Follow the second step to activate the bundle.
Step 2: Enable the Bundle
After installing the bundle from packagist, the autoloader recognizes its classes. Now, you must register the bundle in the app/appkernel.php file.
This is just a dummy bundle. You can add the name and directory path of the new bundle here.
Step 3: Configuration of the Bundle
The process is almost done at this point. However, sometimes, a third-party bundle might require additional configuration in app/config/config.yml. Although the bundle’s documentation also provides the information, But for a quick look you can use config:dump-reference command.
$ bin/console config:dump-reference AsseticBundle
This command provides full information about the bundle.
How to Remove a Bundle
You now know how to create and install bundles in Symfony 3.1. You can also easily remove them from the project. To remove a bundle, just follow the simple steps:
Important: Before removing a bundle, make sure that there are no dependencies. If you remove a bundle, all dependent bundles might fail or start performing erratically.
Step 1: Unregister the Bundle
Previously, you registered the bundle in appKernel.php. For removing a bundle, you have to unregister the bundle. To do this, simply comment out the object of bundle in the array. For instance:
// new FirstBundle\FirstBundle(),
Step 2: Remove Configurations
Symfony now does not know about the removed bundle. Hence, you have to remove all the configurations from the config folder. Remove the bundle resource from config.yml file
Step 3: Remove the Bundle Routing
The next step is to remove the routing configurations in routing.yml file that is found in the config folder
Delete the above lines to remove routing configuration.
Step 4: Removing the Bundle From Directory
So now, all the references and configurations have been removed, the last important thing to do is to remove the bundle folder from the project directory. It is usually found in the source folder at the root directory
Remove this folder from the listing. Remove the assets folder from the web/bundles.
The bundle is now successfully removed.
In this blog, I discussed the management of the bundles with emphasis on creation and deletion of bundles. Bundles play a great role in rapid application development and many of them are available on official Symfony packagist. I hope you have clearly understood the management of the bundles.
In the final part of this series, I will guide you in the process of transferring your Symfony 3.1 app from a cPanel based hosting to Cloudways managed servers. Here is a short introduction and list of major topics in this Symfony 3.1 series.
If you have a question or query, please leave a comment below.
Start Growing with Cloudways Today.
Our Clients Love us because we never compromise on these