Menu

One class per URL: Magento 2 controllers vs Laravel invokable controllers
📰

One class per URL: Magento 2 controllers vs Laravel invokable controllers

DEV Community: laravel·Nikita Sirenko·7 days ago
#6fyESkqX
#devtolaravel#devto#dev#class#controller
Long article (800 words) — no AI summary yet.
Reading 0:00
15s threshold

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>

Enter fullscreen mode

Exit fullscreen mode

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.