4

I am trying to implement Magento 2 Coding Standard so I found an Article Here -> https://community.magento.com/t5/Magento-DevBlog/Decomposition-of-Magento-Controllers/ba-p/430883

I have changed my frontend And backend Controllers in the same way

Here is an Example

Frontend Controller:

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\RedirectFactory;

class HomeRedirect implements HttpGetActionInterface
{
    private RedirectFactory $redirectFactory;

    /**
     * @param RedirectFactory $redirectFactory
     */
    public function __construct(
        RedirectFactory $redirectFactory
    ) {
        $this->redirectFactory = $redirectFactory;
    }

    /**
     * Check is Allowed
     * @return bool
     */
    protected function _isAllowed()
    {
        return true;
    }


    public function execute()
    {
        $this->redirectFactory->create()->setPath('dashboard');
    }
}

, in the same way, I am have implemented an admin controller

use Magento\Backend\Model\View\Result\Forward;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Backend\Model\View\Result\ForwardFactory;

class NewAction implements HttpGetActionInterface
{
    private ForwardFactory $forwardFactory;

    /**
     * @param ForwardFactory $forwardFactory
     */
    public function __construct(
        ForwardFactory $forwardFactory
    ) {
        $this->forwardFactory = $forwardFactory;
    }

    /**
     * @return Forward
     */
    public function execute()
    {
        return $this->forwardFactory->create()->forward('edit');
    }
}

Is this right way to do so?

Anyone, please suggest to me any Module in which the composition is implemented on controllers properly

2
  • I think, for now, composition principle is more about writing your custom code. But you are thinking right. it's a bit more complicated, read some blogs about composition, like: yireo.com/blog/…. Magento core cannot rewrite all code from inheritance to composition, it's too massive and complicated. p.s. For custom controllers, from latest magento versions, you should only implement interfaces: HttpGetActionInterface, HttpPostActionInterface or just ActionInterface Commented Nov 15, 2022 at 22:12
  • we had a discussion on github about this you can check it github.com/magento/magento2/issues/34748
    – Waqar Ali
    Commented Nov 16, 2022 at 6:10

0

Your Answer

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

Browse other questions tagged or ask your own question.