-1
<?php
declare(strict_types=1);
namespace Module\Coord\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
 protected $resultPageFactory;
 /**
 * Constructor
 *
 * @param \Magento\Framework\App\Action\Context $context
 * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
 */
 public function __construct(
 \Magento\Framework\App\Action\Context $context,
 \Magento\Framework\View\Result\PageFactory $resultPageFactory
 ) {
 $this->resultPageFactory = $resultPageFactory;
 parent::__construct($context);
 }
 /**
 * Execute view action
 *
 * @return \Magento\Framework\Controller\ResultInterface
 */
 public function execute()
 {
 return $this->resultPageFactory->create();
 }
}
asked Nov 13, 2024 at 16:53

1 Answer 1

0

Deprecate the use of Magento\Framework\App\Action\Action and Magento\Framework\App\Action\Context, you can replace them with Magento\Framework\App\Action\HttpGetActionInterface (for GET actions) or Magento\Framework\App\Action\HttpPostActionInterface (for POST actions). This approach avoids inheriting the Action class and its constructor by defining the controller class as an implementation of HttpGetActionInterface or HttpPostActionInterface.

Updated Code

  • Implement HttpGetActionInterface instead of extending Action.
  • Remove the parent::__construct call.

Explanation

  1. HttpGetActionInterface: Implements the appropriate interface rather than extending Action.

  2. Removal of Context and parent::__construct call: The Context parameter is no longer needed, simplifying the constructor.

Code:

<?php
/**
 * Copyright © 2024
 * All rights reserved.
 * See COPYING.txt for license details.
 * Check module
 * Magento 2.4.7-p3
 */
declare(strict_types=1);
namespace Module\Coord\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\PageFactory;
/**
 * Class Index implements HttpGetActionInterface
 *
 * @class Index
 * @since 11/13/2024
 * @version 1.0.0
 */
class Index implements HttpGetActionInterface
{
 /**
 * Constructor
 *
 * @param PageFactory $resultPageFactory
 */
 public function __construct(
 protected PageFactory $resultPageFactory
 ){
 }
 /**
 * Function execute view action
 *
 * @return ResultInterface
 */
 public function execute(): ResultInterface
 {
 return $this->resultPageFactory->create();
 }
}
answered Nov 13, 2024 at 16:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.