Magento, PHP, Java, C#

Month: January 2016

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 ↑