π 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! π