I have a module that programmatically creates new products in Magento 2.3.2 with multiple store views. This Magento version has an issue that causes the product URL to be correct only for the default store view. In order to solve this, I installed the OlegKoval RegenerateUrlRewrites extension that adds a console command to add the missing URLs in the database. The extension works: I run the extension console command and the URLs are fixed.

So what I'm doing right now is:

  • the method is called and generates all the new products
  • I manually run OlegKoval command to fix the URLs

What I'd like to achieve:

  • the method is called and generates all the new products
  • the same method then calls the OlegKoval command to fix the URLs so that I don't have to run the command manually

Is there a way to achieve this?

  • i added solution ): – Chandresh Chauhan Oct 16 '19 at 13:45
  • Thank you. Unfortunately I got an error. The command I run manually is php bin/magento ok:urlrewrites:regenerate so I tried $output = shell_exec('php bin/magento ok:urlrewrites:regenerate'); $this->log($output); but the log shows Could not open input file: bin/magento – Lounik Oct 17 '19 at 14:22
  • can you check modified answer – Chandresh Chauhan Oct 18 '19 at 4:01
  • @Lounik use an absolute path – krybbio Sep 4 '20 at 15:59

You can try this for run OlegKoval command in observer

$output = shell_exec('php bin/magento cache:clean');

you can change your command in this, you can try below code in observer file

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
    $url = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

    $postUrl = $url.'command.php';
    $curl = curl_init();
            CURLOPT_URL => $postUrl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 100,
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,

    $response = curl_exec($curl);
    $err = curl_error($curl);

== create command.php file at the root in magento


use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$output = shell_exec('php bin/magento cache:clean');

also refer this link may help you https://www.google.com/search?q=enable+exec

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.