Since Magento strictly discourages the use of ObjectManager, there are service classes for abstracting it for all scenarios. Thus, you should use factory for all models (non-injectables):
You only have to ask a desired model’s factory in a constructor. Hence, it will be automatically generated, while you run compiler or Magento. Continue Reading
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 AllowOverride All is situated in a proper <Directory> directive. And don’t forget to check if the Apache process user has write permissions to all necessary directories. Continue Reading
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
In the same place, create a TestApp.phpfile with the following content:
PHP
1
<?phpclassTestAppextends\Magento\Framework\App\Httpimplements\Magento\Framework\AppInterface{publicfunctionlaunch(){//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.phpfile in a browser to execute everything from TestApp::launch().
The createApplicationmethodfrom the bootstrap class creates an application class instance and expects the implementation of \Magento\Framework\AppInterfacethat contains 2 methods.
You create your own class in TestAppto implement the interface. Since the catchExceptionmethod 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 runmethod 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::runinits 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::runcalls $response->sendResponse();,where under $responsewe mean everything what the launchmethod returns. Thus, return $this->_response;is required, as it returns an empty response.
The above app class extends\Magento\Framework\App\Httpso you get request, response, and other parameters, but you can omit this by making your class extend nothing. 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:
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.
In Magento 1, for instance, this code was more simple:
PHP
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:
PHP
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.