1

I have seen a sample module with a class like following.

My question is:
1) Why do it need to extends \Magento\Framework\App\Action\Action since $context is not used in execute?
2) Why parent::__construct($context) and not parent::__construct($context, $session, $order)

class SampleClass extends \Magento\Framework\App\Action\Action
{
    protected $order;
    protected $session;


    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $session,
        \Magento\Sales\Model\Order $order,
    )
    {
        $this->session = $session;
        $this->order = $order;
        parent::__construct($context);
    }

    public function execute()
    {
        // execute code here
    }
}
1
  1. You need to extend \Magento\Framework\App\Action\Action when you're implementing a frontend controller class. It doesn't matter if you are using $context or not, you need to extend this abstract class and you'll have to implement execute method.
  2. parent::__construct($context) because this is the constructor signature of \Magento\Framework\App\Action\Action. $session and $order are properties of SampleClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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