bpoiss

Magento, PHP, Java, C#

Category: Magento2

How to debug static file resolution in Magento 2

I guess every Magento 2 developer will run into this common problem at some point. You want to replace a static content file from the core,  you copy it into your theme directory and make your changes, and in the end nothing happens, because Magento still serves the static file from the core. The first thing you can do in your development environment is to delete the deployed static content file.  If Magento 2 is in developer mode, you can just make a new request to the frontend and the file will be automatically redeployed. If it is still not working, this can become a serious problem. In the next steps I want to explain how to debug the static file resolution to track down the issue.

Continue reading

Dependency Injection in Magento 2: Object Argument replacement vs. Class Preference

Dependency Injection

If you never heard about dependency injection in Magento 2 you should first take a look at the Magento 2 documentation about dependency injection. If you checked out the the documentation or already are familiar with dependency injection in Magento 2, you will know that there are two ways to inject objects to your constructors. Take the following code as example:

// Vendor\Namespace\Model\SomeClass
// ...
public function __construct(ExampleClass $object) {
    $this->object = $object;
}
// ...

If you want to inject “$object”, one approach is to use argument replacement, another one is to use class preference. I will shortly describe these two methods and than point out the big difference between them.

Continue reading

Magento2 xml validation on the fly

If you want your PhpStorm IDE to provide Magento2 xml validation on the fly, you now need to set up a misc.xml file in your .idea folder. In your misc.xml file you can configure how the URLs are mapped to the file locations of the Magento2 XSD files. Take the following xml as example:

<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>

The URL is “urn:magento:framework:Module/etc/module.xsd”. The place where PhpStorm is looking for a mapping to any file is the misc.xml file. Since you have not created one, or not inserted the mappings, it will not find the XSDs and on the fly validation won’t work.

Magento2 xml validation

There are different ways to let your IDE validate Magento2 XML files on the fly. If you have not installed the Magento2 PhpStorm Plugin already, I would recommend to use the bin/magento command line tool.

Continue reading

Magento2 module – simple example

In this post will cover the topic of how to create a simple Magento2 module. Aim of this article is to create a simple module with minimal configuration of controller, block and template. Therefore we have to do go through the following points:

  • Create the modules file structure
  • Configure the module
  • Create register.php
  • Define a frontname
  • Setup a controller
  • Create a block
  • Create a template

The aim of the module is to request a certain URL that calls a controller action and renders a template file with it’s own block class.

You can download the module on Github, or follow the instructions in this post to create your own.

Continue reading

© 2019 bpoiss

Theme by Anders NorénUp ↑