0

How to call the controller method from model?

Or

How to programmatically reindexing using API?


Here is my code.Below code gives me error like below.

{ "messages": { "error": [ { "code": 500, "message": "Fatal Error: 'Class Inchoo\Hello\Model\Hello contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Magento\Framework\App\ActionInterface::execute)' in 'C:\new\xampp\htdocs\ChoiceWholesale\app\code\Inchoo\Hello\Model\Hello.php' on line 5", "trace": "Trace is not available." } ] } }

<?php
namespace Inchoo\Hello\Model;
use Inchoo\Hello\Api\HelloInterface;

class Hello extends \Magento\Framework\App\Action\Action implements HelloInterface
{

    protected $indexFactory;
    protected $indexCollection;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Indexer\Model\IndexerFactory $indexFactory,
        \Magento\Indexer\Model\Indexer\CollectionFactory $indexCollection
    )
    {
        $this->indexFactory = $indexFactory;
        $this->indexCollection = $indexCollection;
        return parent::__construct($context);
    }

    public function name() {

        $indexerCollection = $this->indexCollection->create();
        $indexids = $indexerCollection->getAllIds();

        foreach ($indexids as $indexid)
        {
          $indexidarray = $this->indexFactory->create()->load($indexid);

          //If you want reindex all use this code.
           $indexidarray->reindexAll($indexid);

          //If you want to reindex one by one, use this code
           $indexidarray->reindexRow($indexid);

           echo "Reindexing Id :: " .$indexid ."<br>"; 

        }
        echo "Reindexing Completed";
        exit;

    }
}
0

Using API indexing is not a good Idea.

As Indexing run on the command line and it takes much usage on memory & time, So it, not a good idea.

If you want then change the indexing status of each indexing to pending state and Magento cron automatically do indexing as it was in pending. That is the best Solution.

Create di.xml at app/code/Sark/CustomApi/etc/ and where you will define a model class CustomRepository at app/code/Sark/CustomApi/Model/ which overrides Sark\CustomApi\Api\CustomRepositoryInterface.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Sark\CustomApi\Api\CustomRepositoryInterface" type="Sark\CustomApi\Model\CustomRepository"/>
</config>

And CustomRepository.php code is

<?php

namespace Sark\CustomApi\Model;

class CustomRepository implements Sark\CustomApi\Api\CustomRepositoryInterface
{
    /**
     * @var \Magento\Framework\Indexer\IndexerRegistry
     */
    private $indexerRegistry;
    /**
     * @var \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory
     */
    private $stateCollectionFactory;

    public function __construct(
        \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
        \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory $stateCollectionFactory
    ) {
        $this->indexerRegistry = $indexerRegistry;
        $this->stateCollectionFactory = $stateCollectionFactory;
    }

    public function create()
    {
        $indexerStates = $this->stateCollectionFactory->create();
        foreach ($indexerStates->getItems() as $indexerState) {
            $indexerState->getIndexerId();
            $indexer = $this->indexerRegistry->get($indexerState->getIndexerId());
            if ($indexer->getId() && !$indexer->isWorking()) {
                $indexer->setScheduled(true);
            }
        }
    }
}
  • Con you please describe the $indexer->setScheduled(true); line? Why it is used? – AKM Sep 23 at 9:56
  • It's give me error.prnt.sc/p9qqgj – AKM Sep 23 at 10:02
  • Error is from Inchoo sir not from my code. You need to put one code at time. – Mayur Sep 23 at 10:05
  • In Which line , How to solve this error? – AKM Sep 23 at 10:06
  • You need to check where you put this code and remove it and use at a time one code only. – Mayur Sep 23 at 10:07

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.