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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
composer create-project symfony/skeleton mySymfonyApp
composer create-project symfony/skeleton mySymfonyApp
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cd mySymfonyApp
symfony server:start
cd mySymfonyApp symfony server:start
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
hello:
path: /hello
controller: App\Controller\HelloController::index
hello: path: /hello controller: App\Controller\HelloController::index
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
php bin/console make:controller HelloController
php bin/console make:controller HelloController
php bin/console make:controller HelloController

Try It Now

📁 This creates: src/Controller/HelloController.php.

Now, open the controller and modify the function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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!');
}
}
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!'); } }
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{# templates/hello.html.twig #}
<title>Hello Symfony</title>
<h1>Hello, {{ name }}!</h1>
{# templates/hello.html.twig #} <title>Hello Symfony</title> <h1>Hello, {{ name }}!</h1>
{# templates/hello.html.twig #}



    Hello Symfony


    

Hello, {{ name }}!

Try It Now

Modify the controller to render this template:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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]);
}
}
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]); } }
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! 🚀