Symfony is a PHP framework that is open source and developed for online applications. This post will go through how Symfony came to be, major components, architecture, workflow, basic installation processes, and more.
It has established itself as a web service foundation. Reusable PHP libraries and components are included in Symfony. Symfony components are used in applications by projects such as Laravel and Drupal.
Symfony was created to minimize the time it takes to create and maintain web applications and to eliminate recurring operations. The Spring framework has had a significant influence and inspiration on it.
History of Symfony
SensioLabs released Symfony as open-source software on October 18, 2005. It was distributed under the MIT license. However, a more reliable initial version was launched in 2007, but it was not as promising as version 2.0. However, engineers discovered significant problems with the update and backward compatibility. 2.3 was the most promising version, with strong stability and features. The subsequent updates, 2.4, 2.5, and 2.6, were less noticeable, and all subsequent versions were backward compatible. Pseudo-localization, 24-bit colors on the console, TranslatableMessage objects, support for PHP 8 attributes/annotations, and other enhancements are included in the current version, which is 5.2.
As previously said, the primary goal of Symfony is to improve online applications. Symfony frameworks contain the following distinguishing characteristics:
- Micro-framework based on MVC (Model-View-Controller)
- Highly performant PHP framework
- Components that may be reused and are decoupled
- Error logging and session management are available.
- URI routing that is adaptable
- A thriving, vibrant community
- Twig template engine with a high level of maturity
- Code readability and extensibility
- Multiple database systems are supported.
- Architecture based on Symfony
The architecture is straightforward, with reusable components and bundles.
Components give specific functionality in a single area. For instance, the HTTP component, the cache component, the filesystem component, and so on. These are just standalone PHP libraries.
Bundles are similar to plugins in that they are easy to build and utilize. A bundle might include several Symfony components and offer a variety of functionality. A Symfony core web framework is a bundle in and of itself (FrameworkBundle).
Components have access to an unlimited number of bundles. FrameworkBundle, FrameworkExtraBundle, and DoctrineBundle are the most frequent bundles used in the framework. Aside from that, we have the option of creating custom bundles and doctrine bundles.
Components and bundles can be accessed individually by the Symfony application.
Aside from FrameworkBundle, the following components should be mentioned:
This component contains the basic elements needed to construct quick and adaptable HTTP-based applications.
Drupal, OroCRM, Lumen, Thelia, Sulu, and other projects have used the HttpKernel component. Internally, the kernel establishes a process that begins with a request and concludes with a response. It uses the handle() function to resolve the controller, parameters, and response. All of the phases in the HttpKernel process are driven by events.
The HttpFoundation component is in charge of incorporating an object-oriented layer into the HTTP standard. An object-oriented layer surrounds the global variables that are part of every HTTP request, such as GET, POST, COOKIE, SESSION, and so on.
To understand DoctrineBundle, we must first understand Doctrine.
Doctrine is a PHP library project or collection that provides persistent services. It includes an object-relational mapper (ORM) and a database abstraction layer (DBAL). It employs the Doctrine Query Language (DQL), the object-oriented alternative to SQL to construct database queries. Doctrine eliminates the need to store and manage complicated XML database schemas.
The DoctrineBundle combines Symfony applications with the ORM and DBAL layers. A collection of terminal commands, configuration settings, and a web debug toolbar collector are also included.
How to Download and Install Symfony
PHP 5.4 or later is required to install Symfony on your PC. Aside from that, a web server such as WAMP or Microsoft IIS for Windows, MAMP for Mac, LAMP for Linux, or XAMP/Nginx for Linux is recommended (Multi-platform).
Simple Symfony Workflow
The Symfony framework provides all of the functionalities required for a business application. Here is an example of how a Symfony web application works:
A typical workflow might include the following steps:
The user submits an application request through the browser using the HTTP protocol, e.g., http://xyz.com.
In turn, the browser routes the request to a web server such as Apache, WebSphere, or others.
The server then routes the user’s request to the PHP layer. In turn, the PHP layer routes it to the Symfony web framework.
The essential component HttpKernel resolves the request’s controller (as shown in the architecture). The routing component is used to route the request to the destination request controller.
The controller layer is where the majority of the business logic is executed.
The DoctrineORM then enters the picture, via which the controller communicates with the Model. The connection to the data source is the responsibility of the DoctrineORM.
When the procedure is finished, the controller either generates a response on its own or generates one through the View engine. The view engine is just a component in charge of transforming the response into HTML format.
The web server receives the response. Subsequently, the answer is delivered to the user by the webserver (browser).
Symfony Framework Advantages
Symfony has the following advantages:
Fast-Paced Development: Symfony is a component-based framework. It is simple to install and use. This speeds up and streamlines development, allowing developers to create apps rapidly.
Flexibility: With features like dependency Injection (DI) and event dispatching, Symfony allows developers to easily design extremely complicated applications with the greatest degree of customization, enabling code flexibility.
Developers may reuse bundles or contribute to the framework because everything is a bundle and offers unique functionality. There is no need to alter the framework; only the bundle may be customized to meet the needs of a given project.
Stability: After the first hiccups, the latest Symfony versions have proven highly reliable and long-lasting. They are also compatible with publicly available APIs.
Ease of Development: Developers don’t have to bother about minor functionality and concentrate on their business logic while the framework handles the essential functions. Symfony’s component and bundle-based design make it suitable for modest to large applications. Web debugging toolbars aid in the resolution of various difficulties throughout the early stages of development, resulting in high-quality deliveries.
Symfony is simple to learn since it comprises components and bundles, and once you understand the fundamental components and bundles, you can quickly construct new apps. Some critical components are as follows:
It offers fast searching files and directories in the specified path by iterating through the files.
2. The filesystem
Provides commands for doing fundamental file operations such as creating a file or a folder, determining whether a file exists, and so on.
This module implements the PSR-0 and PSR-4 class loader standards. It also does class autoloading.
Containers are provided to handle dependencies. The framework makes considerable use of this component via the service container.
Provides PHP programming based on events. Objects can communicate with one another by sending events and listening to them.
We may use Serializer to convert PHP objects into multiple forms such as JSON, binary, XML, and so on, and then convert them back to PHP objects without losing any data.
Provides an expression engine for making code more accessible and concise. ExpressionLanguage has two methods for dealing with expressions: evaluation (the expression is evaluated without being compiled to PHP) and compilation (the expression is compiled so that it can be cached and then evaluated). It supports a wide range of syntaxes, including literals, objects, operators, functions, arrays, and so on.
Provides powerful tools for processing finite state machines, allowing for advanced PHP programming.
Provides an object-oriented layer to PHP’s basic HTTP request and response objects.
Allows for an easy development of form in a web application.
Takes care of a web application’s full request-response process. It is the foundation of Symfony architecture.
Routing defines which portion of the application should handle a certain request. It converts the current request into a series of pre-defined configuration variables.
There are many more Symfony components, and you may view them all by visiting the official Symfony website.
A bundle is a collection of files and folders organized in a specific way. The layout is designed so that many programs may reuse the bundles. The AppBundle is a bundle that contains the core Symfony application. Other examples of packages are AdminBundle (an admin site), HelloMobileBundle (a hello mobile app), and so on.
The bundle includes the following items:
Controller: The controller folder contains all of the controllers.
Dependency Injection: This folder contains all files related to DI and configuration.
Resources/config: for configurations related to bundles.
Resources/public: CSS, JS, images, and other bundle-related files belong here.
Resources/view: for storing all view templates associated with the bundle.
Tests: This folder contains all of the bundle unit tests.
You may quickly modify a component of any third-party bundle that you want to employ for your application.
We’ve covered all you need to know to get started with Symfony. The article should have piqued your curiosity in Symfony, and you should be capable of grasping the framework’s fundamental ideas. To learn Symfony, you must first be familiar with the PHP framework.