Magento 2 Routing Rewrite Controller

We can rewrite controller with the help of router. Magento provides the attribute before/after to config the module sort order which define what module controller will be find first.

For example, if we want to rewrite the controller customer/account/login, we will define more route in the route.xml like this: Continue reading Magento 2 Routing Rewrite Controller

Magento 2 Events

As you have heard, Magento 2 is using the events driven architecture which will help too much to extend the Magento functionality. We can understand this event as a kind of flag that rises when a specific situation happens. .

Dispatch event

In Magento 2 Events List, we can use the class Magento\Framework\Event\Manager to dispatch event. For example, we create a controller action in PHPCodez_First to show the word “Hello World” on the screen: Continue reading Magento 2 Events

Recurring Magento 2

Recurring script can be used to run some script that need to executed whenever we issue php bin/magento setup:upgrade irrespective of version change.

The recurring script will be run after the module setup script every time the command line

php bin/magento setup:upgrade

This script will be defined same as InstallSchema class but difference in name of the class. The example for this class you can see in vendor/magento/module-indexer/Setup/Recurring.php

InstallData Magento 2

The InstallData will be run during the module install and its used to load initial data to DB table.

The module install script will run when you run the following command line

php bin/magento setup:upgrade

File : app/code/PHPCodez/First/Setup/InstallData.php

<?php
 namespace PHPCodez\First\Setup;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;
 class InstallData implements InstallDataInterface {
   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
   $setup->startSetup();
   $setup->getConnection()->query("INSERT INTO phpcodez_post SET name = 'Post 1'");
   $setup->endSetup();
 }
 }