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(). There's no route list for controllers. 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, because that interface extends HttpHeadActionInterface. 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, or to the home page when there's no referer. 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. The core itself hasn't caught up: in Magento 2.4.9, 567 core controllers implement an action interface, 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 table, 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.
