Posts

Showing posts from 2019

Magento 2 How to create custom Log file ?

Assuming your module is in YourNamespace/YourModule: 1) Write Logger class in Logger/Logger.php: <?php namespace YourNamespace\YourModule\Logger; class Logger extends \Monolog\Logger { } 2) Write Handler class in Logger/Handler.php: <?php namespace YourNamespace\YourModule\Logger; use Monolog\Logger; class Handler extends \Magento\Framework\Logger\Handler\Base {     /**      * Logging level      * @var int      */     protected $loggerType = Logger::INFO;     /**      * File name      * @var string      */     protected $fileName = '/var/log/myfilename.log'; } Note: This is the only step which uses Magento code. \Magento\Framework\Logger\Handler\Base extends Monolog's StreamHandler and e.g. prepends the $fileName attribute with the Magento base path. 3) Register Logger in Dependency Injection etc/di.xml: <?xml vers...

Magento 2 call static block in phtml file

Method to call CMS static block in phtml file in Magento 2 Call your static block in phtml file: <?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('your_block_identifier')->toHtml(); ?> Display CMS Static Block In CMS Content: {{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"} Display CMS Static Block In XML: <referenceContainer name="content">      <block class="Magento\Cms\Block\Block" name="block_identifier">          <arguments>              <argument name="block_id" xsi:type="string">block_identifier</argument>          </arguments>      </block>  </referenceContainer> Happy Coding :)

Magento 2 SSH Commands

Below is the list of most important SSH / CLI commands for Magento 2 that I have found useful. To use these commands you will need to have SSH access to your server or use the Command Line for local access. Setup Upgrade Using Command Line php bin/magento setup:upgrade If you don’t want to remove pub/static files while installing/updating database then use following command. php bin/magento setup:upgrade --keep-generated Cache Clean Using Command Line php bin/magento cache:clean Cache Flush Using Command Line php bin/magento cache:flush View cache status Using Command Line php bin/magento cache:status Enable Cache Using Command Line php bin/magento cache:enable [cache_type] Disable Cache Using Command Line php bin/magento cache:disable [cache_type] Static Content Deploy Using Command Line (Use -f for force deploy on 2.2.x or later) php bin/magento setup:static-content:deploy Static Content Deploy For Particular Language Using Command Line php bin/ma...