1

Now that we're not supposed to extend Action\Action anymore, is the current best practice to create a separate Controller for each CRUD action? One for create, one for delete etc?

2 Answers 2

0

That will depends on your needs and the way you have architected your implementation but as a general rule, instead of extending Action\Action you are now required to implement an interface.

For example:

If the controller is responsible for rendering content then you should use:

Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;

class Balance implements HttpGetActionInterface
{

    private \Magento\Framework\Controller\ResultFactory $resultFactory;

    public function __construct(\Magento\Framework\Controller\ResultFactory $resultFactory)
    {
        $this->resultFactory = $resultFactory;
    }

    public function execute(): \Magento\Framework\Controller\ResultInterface
    {
        $result = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

        return $result;
    }
}

If the controller is responsible for processing submissions, then you could use:

\Magento\Framework\App\Action\HttpPostActionInterface

These 2 are the most commonly used interfaces to implement when creating custom controllers but if none of these fit what you need, have a look inside:

vendor/magento/framework/App/Action/*
0

Starting with Magento 2.3 (I believe), the recommended approach is for the Action controller to implement the corresponding interface(s). Controllers should extend the \Magento\Framework\App\Action\Action class for frontend controllers or \Magento\Backend\App\Action class for admin controllers.

If you have a look at: vendor/magento/framework/App/Action you will find a number of interfaces that are available, for example:

HttpGetActionInterface.php
HttpPutActionInterface.php
HttpPostActionInterface.php
...

Depending on the specific action your controller would implement the corresponding interface(s).

If you're following SOLID principles, you can segregate one action per controller.

One way to have the controllers structured for the CRUD operations:

Index
New
Edit
Delete
Save

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.