11

On Magento 2.4 i found this class extends (extends \Magento\Framework\App\Action\Action) show deprecated.

Ref : Screenshot at end of the question

But Magento core use this code. I mean in core code they extends this class.

As per knowledge if a code get deprecated then on next release it may get removed, so there must be alternative code for the deprecated code. How can we easily found alternative for list of deprecated code in Magento 2?

enter image description here

2 Answers 2

11

This approach is deprecated because of the favor of composition over inheritance.

So instead of:

class YourClass extends \Magento\Framework\App\Action\Action

use:

class YourClass implements \Magento\Framework\App\ActionInterface

and add your logic to the execute() method.

It's true though that there's still a lot of Magento core code that uses the deprecated approach. So this will not be removed soon, if they will ever do.


Edited Date :- 3rd June, 2022 you can use Magento\Framework\App\ActionInterface directly but, as per Magento standard, you should use the HTTP Method interface (GET, POST, PUT, DELETE) as per your request type

  1. \Magento\Framework\App\Action\HttpGetActionInterface
  2. \Magento\Framework\App\Action\HttpPostActionInterface
  3. \Magento\Framework\App\Action\HttpPutActionInterface
  4. \Magento\Framework\App\Action\HttpDeleteActionInterface

These HttpHTTP MethodActionInterface interfaces extends \Magento\Framework\App\ActionInterface.

your sample controller should look like this

<?php 

namespace Vendor\ModuleName\Controller\Index;

use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\PageFactory;

class Display implements HttpGetActionInterface
{
    /**
     * @var PageFactory
     */
    private $pageFactory;

    /**
     * @var RequestInterface
     */
    private $request;

    public function __construct(PageFactory $pageFactory, RequestInterface $request)
    {
        $this->pageFactory = $pageFactory;
        $this->request = $request;
    }

    public function execute()
    {
        // Get the params that were passed from our Router
        // echo $firstParam = $this->request->getParam('first_param', null);

        return $this->pageFactory->create();
    }
}

for more details about routing action class see here at Magento docs

1
  • 1
    Thank you for a simple explanation :)
    – Zankar
    Commented Dec 2, 2021 at 11:11
0

Maybe I am late with the answer.

In Magento 2.4 Action classes are implementations of the ActionInterface that a router returns on matched requests. The execute() function in these classes contain the logic for dispatching requests.

More details can be found here

Your Answer

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

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