PHP Symfony Basics

πŸš€ 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

Try It Now

🎯 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

Try It Now

πŸ” 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

Try It Now

🎯 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

Try It Now

πŸ“ 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!');
    }
}

Try It Now

🎯 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 }}!

Try It Now

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]);
    }
}

Try It Now

🎯 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! πŸš€