3

Is it possible to do Index Management without SSH/Shell Access in Magento ver. 2.1.0 ?

  • In magento 1 you can just access the backend, go to System > Index and regenerate them. Doesn't this work for Mage 2? – Fabian Blechschmidt Oct 1 '16 at 21:16
  • if it did i wouldn't be asking. Even the backend looks totally different now. – mysticalghoul Oct 2 '16 at 5:32
  • @FabianBlechschmidt no you can reindex from M2 backend. Here's a small module I just wrote to do so: github.com/digitalpianism/reindex – Raphael at Digital Pianism Oct 3 '16 at 10:01
5

Module link

After your question I ended up developping a small module for that. It is available here: https://github.com/digitalpianism/reindex

Module Code

But here is the how to:

app/code/DigitalPianism/Reindex/etc/module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="DigitalPianism_Reindex" setup_version="0.1.0">
        <sequence>
            <module name="Magento_Index"/>
        </sequence>
    </module>
</config>

app/code/DigitalPianism/Reindex/etc/acl.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="Magento_Backend::system">
                    <resource id="Magento_Backend::tools">
                        <resource id="DigitalPianism_Reindex::reindex" title="Reindex" translate="title" sortOrder="30" />
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

app/code/DigitalPianism/Reindex/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="reindex" frontName="reindex">
            <module name="DigitalPianism_Reindex" />
        </route>
    </router>
</config>

Now we add a new mass action to the index grid mass actions dropdown:

app/code/DigitalPianism/Reindex/view/adminhtml/layout/indexer_indexer_list_grid.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="adminhtml.indexer.grid.grid.massaction">
            <arguments>
                <argument name="options" xsi:type="array">
                    <item name="reindex" xsi:type="array">
                        <item name="label" xsi:type="string" translate="true">Reindex</item>
                        <item name="url" xsi:type="string">reindex/reindex/reindex</item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Finally we create the controller that will handle the reindex:

app/code/DigitalPianism/Reindex/Controller/Adminhtml/Reindex/Reindex.php

<?php

namespace DigitalPianism\Reindex\Controller\Adminhtml\Reindex;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;

/**
 * Class Index
 * @package DigitalPianism\Reindex\Controller\Adminhtml\Reindex
 */
class Reindex extends Action
{
    /**
     * Check the permission to run it
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('DigitalPianism_Reindex::reindex');
    }

    /**
     *
     * @return void
     */
    public function execute()
    {
        $indexerIds = $this->getRequest()->getParam('indexer_ids');
        if (!is_array($indexerIds)) {
            $this->messageManager->addError(__('Please select indexers.'));
        } else {
            try {
                foreach ($indexerIds as $indexerId) {
                    /** @var \Magento\Framework\Indexer\IndexerInterface $model */
                    $model = $this->_objectManager->get('Magento\Framework\Indexer\IndexerRegistry')->get($indexerId);
                    $model->reindexAll();
                }
                $this->messageManager->addSuccess(
                    __('%1 indexer(s) were reindexed.', count($indexerIds))
                );
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException(
                    $e,
                    __("We couldn't reindex indexer(s)' because of an error.")
                );
            }
        }
        $this->_redirect('indexer/indexer/list');
    }
}

app/code/DigitalPianism/Reindex/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'DigitalPianism_Reindex',
    __DIR__
);

How to

First enable the module:

php bin/magento module:enable DigitalPianism_Reindex
php bin/magento setup:upgrade

Then go to System > Index Management, select the indexes you want to reindex and choose reindex in the mass actions dropdown. Click the submit button and you're done

enter image description here

1

Create deploy.sh file in your project root directory and past following content

#!/bin/bash
rm -rf /var/www/project/public_html/var/cache/ /var/www/project/public_html/var/page_cache/ /var/www/project/public_html/var/generation/ /var/www/project/public_html/var/view_preprocessed/ /var/www/project/public_html/pub/static/adminhtml/ /var/www/project/public_html/pub/static/frontend/ /var/www/project/public_html/pub/static/_requirejs/
/var/www/project/public_html/bin/magento setup:upgrade
/var/www/project/public_html/bin/magento setup:static-content:deploy
/var/www/project/public_html/bin/magento indexer:reindex

now create deploy.php file in your project root directory and past following content

<?php
ob_start();
error_reporting(1);
ini_set('display_errors', 'On');
$log = '';
$file = /*'/bin/bash ' .*/ dirname(__FILE__) . '/deploy.sh';
$cmds = explode("\n", file_get_contents($file));
foreach($cmds as $k=>$cmd) {
if(substr($cmd, 0, 1) == '#') continue;
$log_bfr = '';
    $log_bfr = shell_exec("$cmd");
if(strlen($log_bfr) <= 0)    
 sleep(5);
   $log .= $cmd . '<br>' . $log_bfr;
}
ob_end_flush();
echo $log;
?>

whenever you need reindex ya cache clean you run this file.

  • I ran your code many times but its still not indexing anything :-( – mysticalghoul Sep 30 '16 at 12:07
  • you need to replace this path /var/www/project/public_html/var/cache/ and add your project application path. – Pratik Sep 30 '16 at 12:14
  • I did that: /var/www/vhosts/my-site/my-site-sub-domain/var/ – mysticalghoul Sep 30 '16 at 12:22
  • Do i need to increase server timeout ? – mysticalghoul Sep 30 '16 at 12:53

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.