Laravel calls them invokable controllers. In Magento 2, every controller already is one. What differs is where the URL and the HTTP verb live, and what a wrong verb gets back: a 405 in Laravel, a 302 or a 404 in Magento.
How Magento finds a controller
A Magento controller is one class per URL with one method, execute(). You don't write a route for a controller. routes.xml gives the module a frontName, and the rest of the URL is the folder and the class name, read left to right. /stock/sku/status lands in Controller/Sku/Status.php.
<router id="standard">
<route id="acme_stock" frontName="stock">
<module name="Acme_Stock"/>
</route>
</router>
Product pages go through the same lookup. The url_rewrite table maps paper-test.html to catalog/product/view/id/31, and that path is read the same way: frontName catalog, then Controller/Product/View.php.
The class itself is small: 3 constructor arguments and one execute(). It returns a result object. For ?sku=MB01 that's {"qty":7} with a 200.
// GET /stock/sku/status?sku=MB01 frontName / folder / class
// POST with a form key = 404, without one = 302 (CSRF)
class Status implements HttpGetActionInterface
{
public function __construct(
private RequestInterface $request,
private JsonFactory $jsonFactory,
private StockCheckerInterface $stock
) {}
public function execute(): Json
{
$sku = (string) $this->request->getParam('sku');
return $this->jsonFactory->create()->setData(['qty' => $this->stock->qty($sku)]);
}
}
Laravel: one line per URL
Laravel does the same thing with __invoke and an explicit line in routes/web.php. php artisan make:controller --invokable writes the class. You point the route at the class, and __invoke() gets the StockChecker and the {sku} parameter injected. Same answer: {"qty":7}.
// POST here = 405
Route::get('/stock/{sku}', StockStatus::class);
class StockStatus extends Controller
{
public function __invoke(StockChecker $stock, string $sku): JsonResponse
{
return response()->json(['qty' => $stock->qty($sku)]);
}
}
The verb lives in different places
In Laravel the verb is part of the route. Route::get() registers GET and HEAD, and a POST to the same URL gets a 405.
In Magento you declare the verb in the class: implement HttpGetActionInterface, and the controller answers GET. HEAD comes with it: the HTTP method map in app/etc/di.xml points both GET and HEAD at that interface. The declaration is opt-in. A controller that implements none of these HTTP method interfaces skips the verb check: a GET, a PUT and a DELETE all reach execute(). On the storefront, what a wrong verb gets depends on the order of the request validators, and the CSRF check runs before the verb check.
A POST without a form key never reaches the verb check. It gets a 302 back to the referer when the referer is a URL of the same site, and to the home page otherwise. A POST with a valid form key passes CSRF, fails the verb check and gets a 404. An AJAX POST skips the form key check and gets that 404 too, and so does a PUT.
The base class is optional in both
Magento marks its old Action base class @deprecated 103.0.0 in magento/framework. The docblock says inheritance in controllers should be avoided in favor of composition. A controller can be a plain class that implements an interface and takes everything through the constructor, like the one above. It gives something up: a plugin declared on AbstractAction::dispatch() doesn't run for a controller with no parent class, and the core declares 2 of those for the storefront. The core itself hasn't caught up: in Magento 2.4.9 from the GitHub repository, 564 core controllers name an HTTP method interface in their own class declaration, and 2 of them have no parent class.
Laravel went the same way from the other side. Open app/Http/Controllers/Controller.php in a new Laravel 11+ app: it's an empty abstract class, with no traits and no methods. make:controller still writes extends Controller, but there's nothing left to inherit.
What each one gives you
Magento's upside: for a controller URL, you read the address and you know the file.
Laravel's upside: every URL is written down. php artisan route:list shows all of them in one list, with the verb and the class.
The rule
One action, one class, dependencies in the constructor or the method. In Magento that's how every controller works. In Laravel it's one option of several, next to resource controllers and closures in the routes file, so it's worth choosing on purpose. And when you call a Magento controller with the wrong verb, expect a 302 or a 404, not a 405.
Next: the interface is the API, Magento's service contracts and webapi.xml next to Laravel's routes and API Resources.
Do you keep one class per action in Laravel, or group them into resource controllers?
Part 5 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.
Top comments (0)