PHP Symfony Basics – Getting Started with Symfony Framework
Symfony is a powerful and flexible PHP framework designed for modern web applications. It follows the Model-View-Controller (MVC) architecture and provides reusable components to speed up development. Let’s explore Symfony!
What is Symfony?
Symfony is an open-source PHP framework that provides a structured and scalable way to build web applications.
- Flexible & Modular: Uses reusable components.
- High Performance: Optimized for speed and scalability.
- MVC Architecture: Organizes code efficiently.
- Large Community: Well-documented and widely used.
Step 1: Install Symfony
Installing Symfony via Composer
The easiest way to install Symfony is using Composer. Run the following command:
composer create-project symfony/skeleton mySymfonyApp
This will: Download Symfony and set up a new project folder
mySymfonyApp
.
Step 2: Start the Symfony Development Server
Once installed, navigate to your project folder and start the server:
cd mySymfonyApp symfony server:start
Output: Your Symfony app will be available at
http://127.0.0.1:8000
.
Step 3: Symfony Routing (Creating a Simple Route)
Routes in Symfony define how your application responds to HTTP requests. Open the config/routes.yaml
file and add a new route:
hello: path: /hello controller: App\Controller\HelloController::index
Now, visit:
http://127.0.0.1:8000/hello
to access the route.
Step 4: Creating a Controller
Symfony uses controllers to handle requests. Create a new controller using:
php bin/console make:controller HelloController
This creates:
src/Controller/HelloController.php
.
Now, open the controller and modify the function:
namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class HelloController extends AbstractController { #[Route('/hello', name: 'hello')] public function index(): Response { return new Response('Hello, Symfony!'); } }
Now, visit:
http://127.0.0.1:8000/hello
to see the output.
Step 5: Creating a Twig Template
Symfony uses Twig as its templating engine. Let’s create a new template:
{# templates/hello.html.twig #}Hello Symfony Hello, {{ name }}!
Modify the controller to render this template:
namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class HelloController extends AbstractController { #[Route('/hello/{name}', name: 'hello')] public function greet(string $name = "Symfony"): Response { return $this->render('hello.html.twig', ['name' => $name]); } }
Now, visit:
http://127.0.0.1:8000/hello/John
to see “Hello, John!”.
Summary
Symfony is a robust PHP framework following the MVC pattern.
Routes are defined in
config/routes.yaml
or using annotations.Controllers handle logic and respond to requests.
Twig templates make HTML rendering easy and dynamic.
Next Steps
Now that you’ve learned the basics, try working with Symfony Forms & Databases to build dynamic applications!