1. Call a helper or model   or Locale

 $this->_objectManager->get('Magento\Catalog\Helper\Category')

 $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')

where $this->_objectManager is predefined object.

 

 

2. Get Image url:

 $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');

                        $productImage = $_imagehelper->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(100)->getUrl();

 

3. Get store currency symbl and rate

\Magento\Store\Model\StoreManagerInterface $storeManager,

 

$currencyCode = $this->_storeManager->getStore()->getCurrentCurrencyCode(); // give the currency code

 

$currencyRate = $this->_storeManager->getStore()->getCurrentCurrencyRate(); // give the currency rate

 

4.  Get base url:

$this->_storeManager->getStore()->getBaseUrl()

or

$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')

            ->getStore($storeId)

            ->getBaseUrl();

media base url:

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

for link url:

$this->_storeManager->getStore()

           ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);

Or can get base url in block like this:

$context->getStoreManager()->getStore()->getBaseUrl()

 

for get custom url:

A universal solution: works from anywhere, not only from a template: /** @var \Magento\Framework\UrlInterface $urlInterface */ $urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface'); $urlInterface->getUrl($path);

or

$this->_objectManager->get('Magento\Framework\UrlInterface')->getUrl($path);

 

5. Create block using layout

...

protected $layoutFactory;

...

public function __construct(

   ...

   \Magento\Framework\View\LayoutFactory $layoutFactory;

   ...

) {

   ...

   $this->layoutFactory = $layoutFactory;

   ...

}

$this->layoutFactory->create()->createBlock('Block\Class\Here');

 

6. get block instance from your layout or resultfactory

$resultPage = $resultPageFactory->create();

$blockInstance = $resultPage->getLayout()->getBlock('block.name.here');

 

 

7. registry in magento2

...

protected $layoutFactory;

...

public function __construct(

   ...

   \Magento\Framework\Registry $registry

   ...

) {

   ...

   $this->registry = $registry;

   ...

}

 

$this->registry->register('test_var', 'this is a test!');

 

8. Add error or success msg

$this->messageManager is an object for that which is available default.

 

For success msg:

->addSuccessMessage();

For error:

->addError

 

9. Redirect to url:

public function execute() {

             return $this->resultRedirectFactory->create()->setUrl(rm_request('url'));

}         

Or

return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());

or

return $this->resultRedirectFactory->create()->setPath('catalog/product/', ['_current' => true]);

 

 10. Shortcut for controler

   protected function helper($className) {

        return $this->_objectManager->get($className);

    }

   

    protected function getUrl($path){

        return $this->_objectManager->get('Magento\Framework\UrlInterface')->getUrl($path);

    }

 11. Set a date format while adding a date field in a form

use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;

$dateFormatIso =$this->_localeDate>getDateTimeFormat(\IntlDateFormatter::MEDIUM);

11 Create a Model with Extend magento core model class

 

use Magento\Framework\Model\AbstractModel;

class TEST extends AbstractModel{

 

}

 12 Call model function in any class

 use Companynamespace\ModuleName\Model\FileName;

class Info  {

/* Create a protected variable */

 protected $_FileName;

 public function __construct(

/* Initialize instance of includeed file */

                FileName $_FileName,

       

        array $data = []

    ) {

                        /* pass in Variable */

         $this->_ FileName = $_FileName;

      }

}

/*Now call model function in a file at anywhere */

$this->_FileName->functionName();