Here is an example of how to upgrade magento 2.1.0 to 2.1.5

You can use following steps with composer update:

composer require magento/product-community-edition 2.1.5 --no-update
composer update
rm -rf var/di var/generation
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex
chmod -R 777 var

After upgrade, check your Magento version with the following command:

php bin/magento --version

How to get IPAddress in Magento 2

Jul 20, 2016 11:44:18 AM

Here is a simple way to get IP Address in Magento 2

protected function getIpAddress()
        $om = \Magento\Framework\App\ObjectManager::getInstance();
        $obj = $om->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
        $ip =  $obj->getRemoteAddress();
        return $ip;

How to Install a Magento 2 on localhost

Jul 5, 2016 11:21:41 PM

So many people as beginners of Magento 2 often get problem with Magento 2 installation and Command Line. Today i will share my experience in this job.

I suppose the project named: magento-2.1.0 (relative path looks like xampp/htdocs/magento-2.1.0/)

You install this project follow magento's guide

I just want to instruct the way to use the Command Line.

After you completed Magento 2 Installation, you need to run some command:

- Open Xampp as below screenshot

Of course we have a few ways to run command line but this is my favorite way

Then type:

cd htdocs\magento-2.1.0\bin [Enter]

php magento setup:static-content:deploy [Enter]

php magento indexer:reindex [Enter]

Done!! Your Magento 2 installation completed

If you want to install the extensions for this project, you just need type command:

php magento setup:upgrade [Enter]

php magento static-content:deploy [Enter] ( this command applys to extension including js, css, html files in web directory)


Event and Observer in Magento 2

Jun 15, 2016 9:21:34 PM

 we have to declare event that we will catch in file di.xml and a function on Observer file.

Example: echo module name, controller name, action name of the current module.

Event used:  controller_action_predispatch

1. Declare Event in events.xml (frontend)

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
<event name="controller_action_predispatch">
<observer name="magenhub_event" instance="Magenhub\Chris\Model\Observer" method="printInfo" shared="false" />

This code will be write in module: Magenhub_Chris, model Observer.

2. Write file Observer.php

Write function printInfo to print module name, controller name, action name of the current url.

File: app/code/Magenhub/Chris/Model/Observer.php


namespace Magenhub\Chris\Model;

class Observer {

public function printInfo(\Magento\Framework\Event\Observer $observer) {
$request = $observer->getEvent()->getData('request');
echo $request->getModuleName('catalog').'<br/>';
echo $request->getControllerName('product').'<br/>';
echo $request->getActionName('view').'<br/>';


This code only print module name, controller name and action name to browser.

  • Result for home page:


  • Result for product view page:



It's a Helper

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Filesystem\DirectoryList;

class Resize extends AbstractHelper{

    protected $_filesystem;
    protected $_storeManager;
    protected $_directory;
    protected $_imageFactory;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Image\AdapterFactory $imageFactory

    ) {
        $this->_filesystem = $filesystem;
        $this->_storeManager = $storeManager;
        $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->_imageFactory = $imageFactory;


    public function imageResize(
        $absPath = $this->_filesystem

        $imageResized = $this->_filesystem
        $imageResize = $this->_imageFactory->create();

        $imageResize->backgroundColor([255, 255, 255]);

        $dest = $imageResized ;
        $resizedURL= $this->_storeManager->getStore()
        return $resizedURL;


    public function getNewDirectoryImage($src){
        $segments = array_reverse(explode('/',$src));
        $first_dir = substr($segments[0],0,1);
        $second_dir = substr($segments[0],1,1);
        return 'cache/'.$first_dir.'/'.$second_dir.'/'.$segments[0];

How to use Date Time in Magento 2

May 14, 2016 7:36:56 PM

For beginner who is getting problem with Datetime class that is not like Magento 1x

we just use class: Magento\Framework\Stdlib\DateTime\DateTime

For example: