1

I am using php shell_exec method to run the magento commands in my controller.

Below is my controller code.

<?php 
 namespace Vendor\Module\Controller\Adminhtml\Migrate;
 use Psr\Log\LoggerInterface;
 class Index extends \Magento\Backend\App\Action
{
 protected $resultPageFactory;
 protected $migrationHelper;
 protected $logger;

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    LoggerInterface $logger
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->logger = $logger;
    parent::__construct($context);
}

/**
 * Execute view action
 *
 * @return \Magento\Framework\Controller\ResultInterface
 */
public function execute()
{
    
    $commandCache = "php bin/magento cache:enable";
    shell_exec($commandCache); 
    
    try{
        $m2version = 2.3.4;
        $commandRepo = "composer config repositories.magento composer https://repo.magento.com";
        $this->logger->info('--commandRepo--'.$commandRepo);
        shell_exec($commandRepo);           
        $commandVersion = "composer require magento/data-migration-tool:".$m2version;
        shell_exec($commandVersion);
        
    }catch(\Exception $e){
        $this->logger->info('--exception--'.json_encode($e->getMessage()));
    }
  }
}

In the above code, cache enable command is working, but I am downloading data-migration tool using composer, but that command is not working, if I run the command manually in shell prompt, its working.

Is there anything else I have to make change?

Can someone please help me on this. Thanks in advance!!

17
  • Please also mention what you changed .. also post your code here. Commented Dec 12, 2018 at 18:20
  • @lalitmohan, could you check what is the issue? Commented Dec 18, 2018 at 5:43
  • Have you found the solution for it? Commented Apr 15, 2019 at 6:28
  • Hi @jafarpinjar I think you may need to pass public and private key as Magento requires it while doing this. Commented Dec 8, 2020 at 7:50
  • or you can create auth.json file on root with public and private key then see how it goes. I am not sure about this solution Commented Dec 8, 2020 at 7:51

1 Answer 1

0

Try this:

Step 1: Run which composer command directly on shell. You'll get something like

/usr/local/bin/composer

Step 2: In your controller code replace your composer require ... command with this one

cd /root/folder/of/magento && php -dmemory_limit=2G /usr/local/bin/composer require //package

If this doesn't work than I am afraid you might have to give your vendor folder permissions of 755 or 777 which is quiet risky.

2
  • hello @Arbab, nope same issue, its not working! Commented Dec 16, 2020 at 14:29
  • @jafarpinjar edited my 2nd command try this out Commented Dec 17, 2020 at 7:24

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.