0

This question already has an answer here:

I need to run command php bin/magento indexer:reindex in Controller.

marked as duplicate by Jaimin Sutariya, Teja Bhagavan Kollepara, Jai, Murtuza Zabuawala, Shoaib Munir May 17 at 8:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
  • @RutveeSojitra the problem is that I need to call this command in code – Tomalo May 17 at 6:50
  • means you don't want to reindexing? – Rutvee Sojitra May 17 at 6:51
  • @RutveeSojitra I want to reindex but I want to do this by calling the command. I do not want to wait for the page to refresh whem reindexing – Tomalo May 17 at 6:57
  • @SavanPatel no working I don't want to wait for refresh page – Tomalo May 17 at 7:16
2

Run Reindex command in Controller Put below code into your controller and you can run when you need it

public function reindexAll() {
    $indexerFactory = $this->_objectManager->get('Magento\Indexer\Model\IndexerFactory');
    $indexerIds = array(
        'catalog_category_product',
        'catalog_product_category',
        'catalog_product_price',
        'catalog_product_attribute',
        'cataloginventory_stock',
        'catalogrule_product',
        'catalogsearch_fulltext',
    );
    foreach ($indexerIds as $indexerId) {
        echo " create index: ".$indexerId."\n";
        $indexer = $indexerFactory->create();
        $indexer->load($indexerId);
        $indexer->reindexAll();
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.