Symfony, a leading PHP framework, continues to evolve with its latest version, Symfony 7.2. This powerful framework provides developers with tools to build robust and scalable web applications efficiently. In this guide, we will walk through the steps and commands required to execute and build a web application using Symfony 7.2.

Step 1: Installing Symfony

To start, you need to install Symfony 7.2 on your system. Ensure that you have PHP 8.2 or higher installed along with Composer, the dependency manager for PHP.

Run the following command to install Symfony:

composer create-project symfony/skeleton my_web_app "7.2.*"

This command creates a new Symfony application named my_web_app using the Symfony skeleton. Replace my_web_app with your desired project name.

Step 2: Navigating to the Project Directory

Once the project is created, navigate to the project directory:

cd my_web_app

Step 3: Configuring the Web Server

Symfony comes with a built-in web server for development purposes. Start the server with the following command:

symfony serve

You will see the URL where your application is running, usually http://127.0.0.1:8000. Open this in your browser to verify the setup.

Step 4: Installing Required Bundles

Symfony's flexibility comes from its bundle system. Add essential bundles for your application as needed. For example, to install Doctrine ORM for database management, use:

composer require doctrine/orm

For Twig, Symfony's templating engine, run:

composer require twig

Step 5: Creating a Controller

Controllers handle the logic of your application. Generate a controller with the following command:

php bin/console make:controller HomeController

This creates a HomeController class and a corresponding template file in the templates/ directory.

Step 6: Defining Routes

Symfony uses routes to map URLs to controllers. Open the src/Controller/HomeController.php file and define a route:

#[Route('/', name: 'home')]
public function index(): Response
{
    return $this->render('home/index.html.twig');
}

This route connects the / URL to the index method of the HomeController.

Step 7: Database Configuration

Configure the database in the .env file:

DATABASE_URL="mysql://username:password@127.0.0.1:3306/my_database"

Replace username, password, and my_database with your actual database credentials.

Step 8: Creating Database Entities

Generate an entity with:

php bin/console make:entity

Follow the prompts to define the fields of your entity. Once done, create and apply migrations:

php bin/console make:migration
php bin/console doctrine:migrations:migrate

Step 9: Building the Frontend

Symfony integrates seamlessly with Webpack Encore for frontend assets. Install Encore and required packages:

composer require symfony/webpack-encore-bundle
npm install

Build your assets:

npm run dev

Step 10: Testing the Application

Symfony includes tools for automated testing. Run tests using:

php bin/console test

Conclusion

Building a web application with Symfony 7.2 involves a series of straightforward steps, from installation to testing. With its robust toolset and structured approach, Symfony simplifies the development process, enabling developers to create high-quality applications efficiently. Follow the commands outlined in this guide, and you’ll be on your way to mastering Symfony development.

Start your Symfony journey today and unleash the potential of this powerful framework!