Welcome to the tutorial on creating your first Symfony application. Symfony is a popular PHP framework that helps you build enterprise-level web applications efficiently. In this guide, we will walk you through the process of setting up a basic Symfony application.
Prerequisites
Before you start, make sure you have the following prerequisites:
- PHP 7.2 or higher
- Composer, the dependency manager for PHP
- A web server (e.g., Apache, Nginx)
- A database server (e.g., MySQL, PostgreSQL)
Step 1: Install Symfony
To install Symfony, open your terminal and run the following command:
composer global require symfony/cli
This will install the Symfony CLI globally on your system.
Step 2: Create a New Project
Once Symfony CLI is installed, create a new project by running:
symfony new my_first_symfony_app
This will create a new directory called my_first_symfony_app
with all the necessary files and folders for a new Symfony application.
Step 3: Navigate to the Project Directory
Change into the project directory:
cd my_first_symfony_app
Step 4: Configure the Database
Edit the .env
file in the project root directory to configure your database connection:
# .env
DATABASE_URL=sqlite:///database.sqlite
For this tutorial, we will use SQLite as our database. If you prefer to use another database, you can change the DATABASE_URL
accordingly.
Step 5: Create a Basic Controller
Create a new controller by running:
symfony make:controller App\Controller\DefaultController
This will create a new controller file in the src/Controller
directory. Replace App\Controller\DefaultController
with your desired namespace and class name.
Step 6: Update the Routes
Edit the config/routes.yaml
file to add a route for the homepage:
# config/routes.yaml
home:
path: /
controller: App\Controller\DefaultController::index
Step 7: Implement the Controller
Open the src/Controller/DefaultController.php
file and implement the index
action:
// src/Controller/DefaultController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends AbstractController
{
public function index(): Response
{
return $this->render('default/index.html.twig', [
'title' => 'Welcome to My First Symfony App!',
]);
}
}
Step 8: Create a Basic Template
Create a new Twig template file in the templates/default
directory:
<!-- templates/default/index.html.twig -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
</head>
<body>
<h1>Welcome to My First Symfony App!</h1>
</body>
</html>
Step 9: Run the Application
Run the following command to start the Symfony development server:
./bin/console server:run
Open your web browser and navigate to http://localhost:8000
. You should see the welcome message displayed.
Next Steps
Now that you have created your first Symfony application, you can start building your web application. For more information and tutorials, check out our Symfony documentation.