How to start Developer Mode in Magento 2
To enable Developer Mode in Magento 2, use SetEnv MAGE_MODE “developer” in your .htaccess file. Please note that this method works only in case when
To enable Developer Mode in Magento 2, use SetEnv MAGE_MODE “developer” in your .htaccess file. Please note that this method works only in case when
You can easily enable profiler in Magento 2 by adding SetEnv MAGE_PROFILER “html” to .htaccess. Additionally, it is possible to utilize “csvfile” (is situated in your var/log) or “firebug”. Continue Reading
Create a test.php file in the root of your Magento 2 instance.
1 |
<?php require __DIR__ . '/app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); /** @var \Magento\Framework\App\Http $app */ $app = $bootstrap->createApplication('TestApp'); $bootstrap->run($app); |
In the same place, create a TestApp.php file with the following content:
1 |
<?php class TestApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface { public function launch() { //dirty code goes here. //the example below just prints a class name echo get_class($this->_objectManager->create('\Magento\Catalog\Model\Category')); //the method must end with this line return $this->_response; } public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception) { return false; } } |
Call your test.php file in a browser to execute everything from TestApp::launch().
The createApplication method from the bootstrap class creates an application class instance and expects the implementation of \Magento\Framework\AppInterface that contains 2 methods.
You create your own class in TestApp to implement the interface. Since the catchException method always returns false, your app don’t handle exceptions. If something goes wrong, print it on a screen.
The implemented launch method is called by \Magento\Framework\App\Bootstrap::run. The run method behaves almost the same in spite of what the application passed as a parameter.
$response = $application->launch(); is the only thing that depends on the app. It means that calling \Magento\Framework\App\Bootstrap::run inits the Magento env and calls the launch method from your app. Therefore, put all the dirty code inside this method.
Then, \Magento\Framework\App\Bootstrap::run calls $response->sendResponse();, where under $response we mean everything what the launch method returns. Thus, return $this->_response; is required, as it returns an empty response.
The above app class extends \Magento\Framework\App\Http so you get request, response, and other parameters, but you can omit this by making your class extend nothing. Continue Reading
To check if your Magento 2 module has been successfully installed, use the following code:
1 |
$this->_moduleManager->isEnabled('Vendor_Module') |
This is the most convenient method among available now. Continue Reading
There are several options for adding custom CSS/JS to your Magento 2 modules, and the following one is the easiest. Continue Reading
You can easily flush the cache storage of Magento 2 from the command line. Note that the following script must be executed from the Magento 2 root folder:
1 2 |
find var/* -type f -or -type d | grep -v 'session' | xargs rm -rf && \ rm -rf pub/static/* |
It deletes all content from the var subfolder excluding the.htaccess file and the var/session subfolder. Besides, the script also deletes all content from the pub/static subfolder excluding the.htaccess file.
To create a message to a system log in Magento 2.0, use the code listed below:
1 2 3 4 |
\Magento\Framework\App\ObjectManager::getInstance() ->get('Psr\Log\LoggerInterface') ->debug('message') ; |
In Magento 1, for instance, this code was more simple:
1 |
Mage::log('message'); |
but you can also utilize another approach in case of Magento 2. Since there is a _logger property related to many objects, you can perform the same task by calling inside such objects. Just use the following code:
1 |
$this->_logger->debug('message'); |
Please note that not every object has the _logger property. Thus, this method will not work with all your objects.
Create etc/product_types.xml in your module to add a new product type in Magento 2:
1 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Catalog/etc/product_types.xsd"> <type name="demoproduct" label="Demo Product" modelInstance="Genmato\DemoProduct\Model\Product\Type\Demo" indexPriority="25" sortOrder="25"> <customAttributes> <attribute name="refundable" value="true"/> </customAttributes> </type> </config> |
The second step requires creating the modelInstance:
1 |
/** * @category Genmato * @package Genmato_MageStackProduct * @copyright Copyright (c) 2015 Genmato BV (https://genmato.com) */ namespace Genmato\DemoProduct\Model\Product\Type; class Demo extends \Magento\Catalog\Model\Product\Type\AbstractType { /** * Delete data specific for Simple product type * * @param \Magento\Catalog\Model\Product $product * @return void */ public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product) { } } |
To set a Magento 2 mode, use the following command:
1 |
magento setup:mode:set {mode} [-s|--skip-compilation] |
Remove instruction is available in Magento 2. Please note that the layout merges in order shown in a module.xml/sequence section. Here is the instruction:
1 |
<?xml version="1.0"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd"> <remove name="dashboard"/> </layout> |