© Gunnard Engebreth, Satej Kumar Sahu 2023
G. Engebreth, S. K. SahuPHP 8 Basicshttps://doi.org/10.1007/978-1-4842-8082-9_15

15. Introduction to Symfony

Gunnard Engebreth1   and Satej Kumar Sahu2
(1)
Madison, WI, USA
(2)
Bangalore, India
 

In the previous chapters, you learned how to use Lavarel, which is a web application framework used with PHP. In this chapter, you will focus on Symfony, which is a very popular PHP framework already used to develop websites and applications, including a very nice number of reusable PHP components.

This chapter consists of the following sections:
  • Introduction to Symfony

  • Installing Symfony

  • Creating a Symfony Application

Introduction to Symfony

Symfony is a full-stack framework built using a standard set of reusable components. It’s a project where you can choose to use some of its components or use the full stack.

It was created by Fabien Potencier in 2005 and is sponsored by SensioLabs. In Symfony’s own words, “Symfony is a set of PHP Components, a Web Application framework, a Philosophy, and a Community - all working together in harmony” (https://symfony.com/what-is-symfony)

Breaking down and getting deeper into this summary:

A PHP framework:

A framework, as you know, is a foundational template to build upon. It consists of
  1. 1.

    A toolbox

    This is a set of reusable components in different contexts of security,* validation, processing, session handling, and more. These foundational elements make our job easier.

     
  2. 2.

    A process to do things

    There are frameworks that are flexible enough to have different structures, naming conventions, and control flow as per your wishes, and there are frameworks that have a conventional way of doing things. Symfony falls into the latter category. It requires some initial learning to understand these conventions but, once learned, they make our job easier in terms of using existing components, maintaining them, and easily creating similar structures through automated tools like command-line tools.

     

A philosophy:

Symfony was created from the imagination of web creators at SensioLabs. It was created by web creators for creators. These creators understand the needs of developers creating web applications. Symfony is created under an open source license, making it open to contributions, improvements, and reuse by the open community, thus bringing in ideas from the best minds.

A community:

Symfony is supported and contributed to by the community for the community. Symfony community support includes GitHub, Slack chat, and SesioLabs.

Features of Symfony: Symfony includes a number of key features that distinguish it from other frameworks. It’s a very PHP-flexible framework, which is very important for a PHP web developer. It’s easy to customize, including full-stack and brick-by-brick. Finally, it is very stable and sustainable.
  1. 1.

    PHP framework for web projects

    Symfony helps you quickly create and maintain PHP web applications. It also helps you avoid repetitive coding tasks and manage code controlling and versioning.

     
  2. 2.

    Ease of use

    Symfony is very easy to use because it is very flexible to any developer needs and is also very accessible. It comes with a lot of documentation and is supported by a strong community of professionals. It is also very easy to use for beginners because it includes embedded best practices.

     
  3. 3.

    Stable

    The release cadence of Symfony makes sure to maintain compatibility between minor versions of all releases and also provides three-year support for all major Symfony versions. This enables a stable and sustainable model that you can trust.

     
  4. 4.

    Extensible

    The integral central part of Symfony is made of reusable components that can be used in other projects or frameworks. This enables Symfony to be very flexible and also extensible with changes to the core behavior of the framework. Along with this, Symfony leverages Composer to integrate the ever-growing list of open source packages, thus enabling developers to easily enrich its ecosystem.

     
  5. 5.

    Fast

    Fast is something everyone desires in terms of performance, but it is very hard to achieve. Symfony was built from the start to be fast with an emphasis on performance.

     
  6. 6.

    Dependency injection

    Dependency injection is a core concept. It allows you to instantiate and use different components in the runtime. Symfony uses it to provide a centralized way for different objects to be initialized and provided in your application, thus enabling simplicity and modularity.

     
  7. 7.

    Modular elements

    Symfony provides many out-of-the-box modular components for managing security, sessions, ORM features, forms, and a templating engine that can be included and fitted into use with Symfony with very little effort.

     
  8. 8.

    Profiler tool

    The Profiler tool is part of every web page and is displayed at the bottom of all your pages. It provides profile information in a variety of contexts, which are discussed in coming sections. This is a very important tool in every developer’s toolset. See Figure 15-1.

     
Figure 15-1

Profiler toolset

In Figure 15-1, you can see a toolbar with summary statistics. It’s the development toolbar displayed by Symfony in debug mode and it contains many details for each unique page. Figure 15-2 shows the controller route, API response status, request time, memory usage, errors in form, and error logs.
Figure 15-2

Profiler toolset components for status, time, memory usage, errors, and logs

Other information includes
  • Translation info

  • Security info

  • Twig/template calls

  • Server info

  • Symfony config info

Clicking on any one of them lets you further delve into the details. For example, Figure 15-3 shows that clicking on the request panel reveals further details related to the request.
Figure 15-3

Request panel

On the left panel, you can also access further parts of the details related to the current page you are trying to access.
  1. 1.

    Command line tools

    The Symfony cli and bin commands allow you to create many starter templates like controllers, entity models, and migrations without doing a deep dive into them. In the following sections and coming chapter, you will see how helpful they are, thus making the development experience joyful.

     
  2. 2.

    Documentation and support

    The documentation of Symfony is unparalleled, with all small details from Getting Started guides, installation, and tutorials to API-specific documentations. The open community is always present to answer questions and provide support over the Stack Overflow platform and other forums. If you are more of a bookworm and want to read in detail, they have an online book (https://symfony.com/book) for your reference. They also provide Symfony training through certification coaching, the SensioLabs University eLearning platform, and video tutorials. Symfony also provides a certification, which is widely valued and is a feat to achieve.

     

Installing Symfony

There are a variety of ways by which Symfony can be installed. Refer to https://symfony.com/doc/current/setup.html.

You will be following the simplest one to get you up and running. Please make sure these prerequisites are installed before proceeding:
  • PHP 8.1 or above

  • PHP extensions: Ctype, iconv, PCRE, Session, SimpleXML, and Tokenizer. These extensions come installed by default with a PHP 8 installation.

  • Composer tool to install Symfony and dependent packages

Before you create a basic Symfony project in the next chapter, let’s install the Symfony CLI, which is very helpful in many tasks. Based on the operating system, there are different ways to install it; see https://symfony.com/download.

In a Ubuntu/Debian system, it can be run using the following commands:
curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | sudo -E bash
sudo apt install symfony-cli
The outcome of your Symfony installation is shown in Figure 15-4.
Figure 15-4

Installation of Symfony

Once the Symfony CLI is installed, you can use it to verify if your system meets all requirements for a Symfony application by running the following command:
symfony check:requirements
You should get an output similar to Figure 15-5 if all is good to proceed.
Figure 15-5

Symfony installation verification

Now that Symfony is installed, let’s create your first application.

Creating a Symfony Application

To start a new project with the name of blog-app, run the following command:
composer create-project symfony/skeleton:"6.1.*" basic-starter-app
cd basic-starter-app
composer require webapp

Anatomy of a Basic Symfony Application

Every installed Symfony application comes with a very basic directory structure with reusable components. The basic directory structure is shown in Figure 15-6.
Figure 15-6

Symfony directory structure

Figure 15-6 shows the responsibilities of the respective subdirectories with all the information. The basic Symfony application structure can be found at the Symfony official web page at https://symfony.com/doc/current/bundles/best_practices.html#directory-structure.

Once the project has been created, start it using the following commands:
cd basic-starter-app
symfony server:start
You can now access the app at http://localhost:8000, as shown in Figure 15-7.
Figure 15-7

New app

This should confirm a valid installation and setup of Symfony.

Summary

In this chapter, you learned about Symfony, a modern framework that can be used by you to build websites. It provides a flexible core and an ever-growing ecosystem of packages. You learned when to use this framework and how to use it.

In the next chapter, you will learn how to develop a basic Symfony application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset