1

I have several methods that I want to implement in the controller, but it needs to have the execute() method implemented. How can I execute other methods? if the method that magento reads is execute it? example

public function execute()

public function save()

public function edit()

1

First of all , yes you can create multiple functions in the controller class and call them in your execute method ($this->Save();) But that is not how it is supposed to work.

You can use Helper class for what you want to do , The controller is supposed to have execute method only .

  1. Create a new folder in your module with the name Helper . VendorName\ModuleName\Helper

  2. Create A file named Data.php VendorName\ModuleName\Helper\Data.php with the following code .

    namespace VendorName\ModuleName\Helper;
    
    Class Data  extends \Magento\Framework\App\Helper\AbstractHelper
    
    {
    
       public function save();
    
       public function edit();
    
    }
    
  3. In your controller class in the constructor add this helper class

       namespace VendorName\Controller\Name;
       use Magento\Framework\Controller\ResultFactory;
    
       Class ControllerName  extends \Magento\Framework\App\Action\Action
    
    {
    
     public function __construct (
    
     VendorName\ModuleName\Helper\Data $helper,
     \Magento\Framework\App\Action\Context $context,
     ResultFactory $resultFactory,
    
    ){
    
     $this->helper= $helper;
     $this->resultFactory = $resultFactory;
     parent::__construct($context);
    
    }
    
    public function execute()
    {
     $this->helper->save();
     $this->helper->edit();
    }
    

I know it takes some extra time to do this , but what if you need to use edit function in another controller also ? You can use helper class for such scenarios where you want to make your functions reusable .

Hope my answer helps :)

2
  • Usage of 'helper' classes is a bad practice - devdocs.magento.com/guides/v2.4/ext-best-practices/…. "A helper class that functions as a catch-all for random methods violates the single responsibility principle because it is an attempt to solve multiple problems in a single class. You should refactor your code and move those functions into the appropriate classes." Jun 8 at 13:48
  • Yes you are right Diana Botean , The code can be further refactored to move functions in appropriate classes .which will be right way to do it . I just explained according to the question asked , for the sake of simplicity . Jun 9 at 12:33
1

You can have multiple methods in the same controller, but this will violate the Single Responsibility Principle (from SOLID), according to which, a class should be 'in charge' for doing only one thing.

In the case of the controllers, that's the passing of the request to the eligible components and the response back to the browser.

However, you can see a core example with a controller class having multiple methods declarations in https://github.com/magento/magento2/blob/ed2ffad8f227e253c0aa50393f40478b02310b65/app/code/Magento/Catalog/Controller/Category/View.php and I am assuming there are other examples like this. Judging by the protected function _initCategory() declaration, I would say that this slipped into the core as the platform was rewritten from M1 to M2, sort of a legacy declaration, totally against M2 rules of naming and construction of controllers.

So I would advise against this approach. Keep your controller classes slim and let them do what they do best, namely declare and define the execute() method, while calling other services/ classes with the appropriate logic that it needs.

0

You have to create separate class for each action and each of them should have just an "execute" method.

1
  • I can not do it ? public function execute(){ $this->Save(); }
    – lucas
    Jun 7 at 15:57

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.