Welcome to Symfony Basics! Today, we will begin by understanding what Symfony is and why it's popular among developers.
Symfony is a PHP framework that provides tools and components for building web applications more efficiently. It serves as a foundation for crafting robust, scalable applications.
Why is Symfony Popular?
- Modularity: Symfony is composed of many small pieces called
components
that you can mix and match to build your application just like LEGO bricks. - Community Support: There is a vast community of developers who contribute to Symfony, creating resources, tools, and plugins to make your development easier.
- Flexibility: Symfony can be adapted to fit various types of projects, from small websites to large enterprise-level applications.
Symfony is often used in large-scale web applications like e-commerce platforms due to its modularity and extensive community support.
Now, let's set up our very first Symfony project.
To install Symfony, we use Composer
, a PHP dependency manager. Run the following command in your terminal:
Bash1composer create-project symfony/skeleton my_project_name
This command will create a basic Symfony project within a folder named my_project_name
.
Let’s explore the directory structure of a Symfony project to understand where different parts of the project are located:
public/
: The web root directory containing public-facing files, includingindex.php
. This is where the entry point for the application resides.src/
: The core application source code is stored here. This is where your controllers, entities, and other core components will live.tests/
: This directory contains test scripts for verifying the functionality and behavior of your application.config/
: This directory holds all the configuration files for the application, such as routing, services, and environment settings.var/
: Used for cache and log files, this folder helps with performance optimization and debugging.vendor/
: This directory contains third-party libraries installed via Composer. It includes Symfony components, its dependencies, and any other PHP packages required by your project. This directory plays a crucial role in autoloading classes using Composer's autoload feature. Avoid modifying files in this folder directly, as it's managed automatically by Composer.
The public/index.php
file serves as the front controller, which is the entry point for all incoming requests of your PHP Symfony application. It initializes the Symfony application and handles the requests. Let’s break down its contents:
php1<?php 2 3use App\Kernel; 4 5require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; 6 7return function (array $context) { 8 return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); 9};
-
Autoloader Initialization: The
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
line loads Composer's autoloader, which is responsible for automatically including all the required classes and files used in the application. This means you don’t have to manually include or require every class, as Composer manages it for you. The autoloader ensures all the project’s dependencies are available to the application. -
Lambda Function: The anonymous function (
return function(array $context) { ... };
) creates and returns an instance of theKernel
class. The Kernel is the heart of the Symfony application, managing the request lifecycle (from receiving a request to sending a response). The function takes an array$context
as its argument, which has two arguments:APP_ENV
(the environment the application is running in, like dev, prod) andAPP_DEBUG
(which enables or disables debugging). These values influence how the application behaves, such as whether it shows detailed error messages or optimizes performance for production.
Running the Application:
To run your Symfony application, you can use the following command:
Bash1symfony serve
Alternatively, if you prefer using PHP's built-in server, run:
Bash1php -S localhost:8000 -t public
The application listens on http://localhost:8000
, which is where you can access the running Symfony application in your browser.
Now that we’ve covered the basics, let's move to the practice section and see a basic Symfony application in action.