Monthly Archives: September 2015

How to create a Magento 2 playground test file with an initialised Magento 2 application

- Magento 2

Magento 2 Development

Create a test.php file in the root of your Magento 2 instance.

In the same place, create a TestApp.php file with the following content:

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

Flushing the Magento 2 Cache Storage

- Magento 2

Magento 2 Development

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.

More tips from Magento 2 Developer’s Cookbook

Continue Reading

Writing a message to a system log in Magento 2

- Magento 2

Magento 2 Development

To create a message to a system log in Magento 2.0, use the code listed below:

In Magento 1, for instance, this code was more simple:

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:

Please note that not every object has the _logger property. Thus, this method will not work with all your objects.

More tips from Magento 2 Developer’s Cookbook

Continue Reading

Adding a new product type in Magento 2

- Magento 2

Magento 2 Development

Create etc/product_types.xml in your module to add a new product type in Magento 2:

The second step requires creating the modelInstance:

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to remove block from a Magento 2 layout

- Magento 2

Magento 2 Development

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:

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to clean LESS/CSS Theme cache in Magento 2

- Magento 2

Magento 2 Development

Right in refresh .less files, since it “collects, processes and publishes source LESS files”:

You can also use dev mode during development. According to documentation “Static view files are not cached; they are written to the Magento pub/static directory every time they’re called.”

More tips from Magento 2 Developer’s Cookbook

Continue Reading