The Universal “Soldier” in PHP
How to Tame Any Framework. Step One — The Controller
The PHP ecosystem is fragmented: Symfony, Laravel, Drupal, Yii, Slim…
At first glance, each framework feels like a separate universe with its own rules, magic, and “correct” way of doing things.
But there is good news:
90% of modern PHP frameworks speak the same language.
That language is HTTP and Symfony Components.
In this article, we take the first step toward becoming framework-agnostic and break down the Controller as the common entry point across Symfony, Laravel, and Drupal.
Why Start with the Controller?
The controller is:
- the first point of entry into business logic
- where HTTP becomes code
- the simplest example without framework noise
If you understand:
- how a controller is created
- how it receives data
- what it returns
➡️ you already understand half of any PHP framework.
The Core Idea (This Is Key)
In all three frameworks, the flow is the same:
HTTP Request
↓
Controller::method()
↓
Response
And almost everywhere:
Symfony\Component\HttpFoundation\RequestSymfony\Component\HttpFoundation\Response
Even when the framework tries to hide it from you.
1. Symfony — The Reference Implementation
Symfony is clean architecture, with no historical shortcuts.
Creating a Controller
php bin/console make:controller HelloController
Symfony generates:
// src/Controller/HelloController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class HelloController extends AbstractController
{
#[Route('/hello', name: 'hello')]
public function index(): Response
{
return new Response('Hello from Symfony');
}
}
What Matters Here
- The controller is a plain PHP class
- Routing is defined via PHP attributes
- The method returns a
Response - No magic involved
📌 Symfony is the best learning reference because everything is explicit.
2. Laravel — Symfony with Sugar on Top
Laravel often feels like a completely different world.
It isn’t.
Creating a Controller
php artisan make:controller HelloController
// app/Http/Controllers/HelloController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HelloController extends Controller
{
public function index()
{
return 'Hello from Laravel';
}
}
Routing:
// routes/web.php
use App\Http\Controllers\HelloController;
Route::get('/hello', [HelloController::class, 'index']);
What’s Really Happening
- Laravel automatically wraps strings into a Response
Illuminate\Http\Requestextends Symfony’s Request- A controller is still:
- a class
- a method
- returning a response
📌 Laravel hides complexity — the architecture is the same.
3. Drupal — Symfony in Heavy Armor
Drupal often intimidates developers.
But if you strip away the CMS layers, it is pure Symfony Controller logic.
Controller in Drupal
// modules/custom/mymodule/src/Controller/HelloController.php
namespace Drupal\mymodule\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function index(): Response
{
return new Response('Hello from Drupal');
}
}
Routing (YAML)
# mymodule.routing.yml
mymodule.hello:
path: '/hello'
defaults:
_controller: '\Drupal\mymodule\Controller\HelloController::index'
requirements:
_permission: 'access content'
Drupal Differences
- routes are defined in YAML
- permissions are mandatory
- controllers belong to modules
But Fundamentally
- same
Response - same Symfony components
- same execution flow
📌 Drupal is not complex — it is verbose.
One Controller — Three Frameworks
Business Logic (Identical Everywhere)
public function index(): Response
{
return new Response('Hello World');
}
| Framework | Routing | Controller Location |
|---|---|---|
| Symfony | PHP attributes | src/Controller |
| Laravel | routes/*.php | app/Http/Controllers |
| Drupal | *.routing.yml | Module src/Controller |
Universal Rule #1
If you understand Symfony controllers, you already understand Laravel and Drupal.
The differences are:
- syntax
- abstraction level
- amount of boilerplate
But the architecture is the same.
Conclusion
A universal PHP developer is not someone who masters a single framework.
It is someone who understands the architectural patterns beneath them.
The controller is the first — and most important — step.
Comments