How to display exceptions on a page in Magento 2
The process is almost similar to Magento 1, but you only have to rename local.xml.sample to local.xml within your pub/errors directory. Continue Reading

The process is almost similar to Magento 1, but you only have to rename local.xml.sample to local.xml within your pub/errors directory. Continue Reading

Unfortunately, the process is not as easy as in Magento 1 ($crumbs = Mage::app()->getLayout->getBlock(‘breadcrumbs’);). In Magento 2, it depends on where you are going to instantiate it from. To create an instance from another block, use the following code:
|
1 |
$this->getLayout()->createBlock('Full\Block\Class\Name\Here'); |
from a controller:
|
1 |
$this->_view->getLayout()->createBlock('Full\Block\Class\Name\Here'); |
from a model and a helper:
|
1 |
$this->_blockFactory->createBlock('Full\Block\Class\Name\Here'); |
Please note that in a case of the model you have to create _blockFactory (a protected member), and inject a \Magento\Framework\View\Element\BlockFactory instance in the constructor, assigning it to the member var. For instance:
|
1 |
protected $_blockFactory; public function __construct( ..., \Magento\Framework\View\Element\BlockFactory $blockFactory, .... ){ .... $this->_blockFactory = $blockFactory; .... } |

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):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
protected $pageFactory; public function __construct(\Magento\Cms\Model\PageFactory $pageFactory) { $this->pageFactory = $pageFactory; } public function someFunc() { ... $page = $this->pageFactory->create(); ... } |
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

Take your constructor and inject the model collection factory into it:
|
1 |
protected $mymodulemodelFactory; public function __construct( .... \[Namespace]\[Module]\Model\Resource\[Entity]\CollectionFactory $mymodulemodelFactory, ... ) { ... $this->mymodulemodelFactory = $mymodulemodelFactory; ... } |
It is possible to use it in any class method:
$collection = $this->mymodulemodelFactory->create(); 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 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

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.