Symfony is an open-source PHP framework designed for web applications. It contains reusable PHP libraries and components. Its aim is to reduce creation and maintenance time for web applications and avoid repetitive tasks.
The Framework’s Features
- Model-View-Controller based micro-framework
- It is a high-performance PHP framework
- Readable code
- Error logging and session management features
- Flexible URI routing
- Multiple database platforms supporting
Symfony Architecture
Its architecture is simple, as it consists of reusable components and bundles, which are nothing more than standalone PHP libraries.
A bundle can consist of several Symfony components, while components can access any number of bundles. The common bundles used in the framework are FrameworkBundle, FrameworkExtraBundle, DoctrineBundle. Moreover, the bundles and doctrine bundles can be customized. The components and bundles can be accessed independently.
Several of the components need to be mentioned:
HttpKernel
This component includes the building blocks that allow the creation of fast and flexible HTTP-based applications. It can be installed using the following command:
$ composer require Symfony/HTTP-foundation
The kernel internally defines a workflow that defines a workflow that starts with a request and ends in a response. Events drive all the steps in the HttpKernel workflow, which are the controlling, arguments resolving, and getting the response using the handle() method.
HttpFoundation
This component is responsible for adding the object-oriented layer to the HTTP specification. It can be installed using the following command:
$ composer require Symfony/HTTP-foundation
DoctrineBundle
It is worth mentioning that a doctrine itself is a project or PHP library set that is to provide persistent services. It consists of an Object-Relational Mapper and database abstraction layer (DBAL) and uses the Doctrine Query Language (DQL) for the database queries writing. The need of storing and maintaining complex XML database schemas is removed with Doctrine using. The ORM and DBAL layers are integrated into Symfony applications using the DoctrineBundle. The bundle can be installed via:
$ composer require doctrine/doctrine-bundle
In order to receive a database-specific bundle, the bundle should be added to the composter.json file. For instance, in case you want to add the DoctrineMongoDBBundle, the file will look this way:
"require": { "doctrine/mongodb-odm-bundle": "^3.0" }
After that, the dependencies should be installed. To do that, use the update command from the file’s directory:
$ php composer.phar update doctrine/mongodb-odm-bundle
How to Download and Install Symfony
Your PHP version should be 5.4 or newer for Symfony installation. It is recommended to have a web server, too (e.g. WAMP or Microsoft IIS for Windows, MAMP for Mac, and so on).
The installer can be downloaded directly through the command line via the following commands:
$mkdir -p /usr/local/bin $curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony $chmod a+x /usr/local/bin/symphony
After the installer is downloaded, a new application is to be created:
symfony new
Following that, Symfony will be installed on your system, and the project in the ‘first app/’ directory will be created.
Then the server should be started. This should be done via:
cd PHP bin/console server:run
To check if the installation has been successful, go to localhost:8080 in your browser. It should display the following message:
After the framework has been installed, you are free to add the required components and bundles the way it is shown above.
Simple Symfony Workflow
The standard workflow of a Symfony web application is shown here:
The workflow consists of several steps, which are:
- An application request is made via the browser, using the HTTP protocol – http://xyz.com;
- The browser passes the request to a web server (Apache, WebSphere, etc.);
- The server forwards the request to the underlying PHP layer. The layer redirects it to the Symfony web framework;
- HttpKernel uses the routing component to forward the request to the target request controller after resolving it;
- The main logic is being executed in the controller;
- The controller interacts with the Model through the DoctrineORM, which is responsible for connecting to the data source;
- After all of the previous steps are completed, the controller generates a response itself or creates it via the View engine. View engine is a component that converts the response into an HTML format document;
- The final response is sent back to the webserver and then transferred to the user.
The Framework’s Advantages
- Quick development. Symfony is a component-based framework that hastens the development process since it does not take much time to install and use it.
- Variability. Giving flexibility to the code via features like dependency injection (DI) or event dispatching, Symfony simplifies the development of even complex applications with a higher level of configuration.
- Extensibility. Developers are free to reuse old bundles and add new ones. There is no need to modify the whole framework, too, since the bundles may be configured.
- Steadiness. The latest versions of Symfony are reliable and stabilized and may be integrated with public APIs.
- Development simplicity. Thanks to its bundle-based architecture, Symfony suits all-size-range projects. The framework undertakes the core functionalities, thus the developers should not be concerned about minor things. Symfony’s web debugging toolbars help to solve issues at the early development stage, which ensures an excellent result.
The Framework’s Components
As mentioned above, Symfony is a component and bundles-based framework, which simplifies the learning and applications creating processes. The most important components are:
- Finder. This component makes files and directories searchable by iterating over the files in a mentioned path.
- Filesystem. The component includes commands for performing basic operations with files, e.g. creating a file or a folder.
- ClassLoader. Provides the implementation for PSR-0 and PSR-4 class loader standards. It performs auto loading of classes, too.
- DependencyInjection. This component helps with handling dependencies with its containers.
- EventDispatcher. Provides event-based PHP programming and allows objects to communicate by dispatching and listening to events.
- Serializer. Helps convert PHP objects into different formats, e.g. JSON, XML, and convert them into PHP, too.
- ExpressionLanguage. Helps reduce a code and make it neater. It consists of 2 ways of working with expressions: compilation and evaluation. The component also supports many syntaxes, e.g. objects, operators, arrays, literals.
- Workflow. Provides advanced programming tools for finite state machine processing, which allows advanced PHP programming.
- HttpFoundation. This component is responsible for adding the object-oriented layer to the HTTP specification.
- Form. Eases forms creation in a web application.
- HttpKernel. As mentioned above, the component includes the building blocks that allow the creation of fast and flexible HTTP-based applications.
- Routing. That is the part of the application that is responsible for handling a particular request, mapping it to a set of predefined configuration variables.
The framework’s bundle
A bundle itself is a selection of files and folders that is systematized in a particular structure. The systematization allows multiple applications to reuse the bundles. The core Symfony application is a bundle too, and it’s called the AppBundle. There are also different bundle examples, such as AdminBundle, HelloMobileBundle, etc.
Any bundle consists of the following:
- Controller. All the controllers are located in the controller folder.
- Dependency Injection. All files related to DI are located in the dependency injection folder.
- Resources/config. All the related configuration files are located in this folder.
- Resources/public. Bundle-related CSS, JS, and images are located here.
- Resources/view. This folder is used for storing the view templates that are related to the bundle.
- Tests. The bundle unit tests are stored here.
Any third-party bundle parts can be easily customized for your application.