Dependency Injection

 

 

As you remember, we created our own controller. To do this, we added the front name to the e t c front end routes dot xml file which you can see here, and added it to the default router.And also we created an index folder, which is actually the name of the controller. And the index actions file.So now we can see the result of executing our controller method in the storefront.With the following path: test_front_name we can also use the full path like index/index, but perhaps, as you know, index/index can be excluded as the default value.Later we will discuss in detail the Magento routing system, routers and routing request flow.But now we can use this controller for testing purposes.Let's say you need to use some functionality of some external class here.Let's create a special class in our new module. Magento uses the PSR-4 standard, so we can specify something like a vendor name, a module name, and create any custom structure inside that module folder.Let's create something like "some folder name""some class name"And let's add some PHP code here.Let's also add some public method here, something like someMethod.And let's return some results.Now we can create an object of this class in our controller.Let's do it.To do this, we can specify the fully qualified class name.Here.Let's create someObject variableLet's get Class Name.It works fine, let's check our method.It works fine, which is great, but hard-coding such dependencies is not the best practice.To follow best practices, we should use abstractions in our code to reduce code dependencies.This means that our high-level classes should use the interfaces of the low-level classes rather than working with them directly.Let's create an interface for our new class as we are doing now.So let's add some PHP code here.And let's add an implementation to our classAdditionally, using interfaces in our code reduces the risk of incompatibility errors when Magento changes some underlying implementation of those interfaces.It also allows you to focus on what the class does rather than how it is implemented.We should definitely use interfaces instead of working directly with classes as we do now in the index action.Dependency injection is a technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally.Let's create di.xml file in a "e t c" folder.As you remember, this can be either the frontend or the administrative area.But we're going to use the base area to be able to use this class in both the admin panel and the storefront.Let's take an example of such a file from the magento catalog module.Let's remove all unnecessary data here, leaving only one preference for testing purposes.Let's specify our interface and some concrete implementation of this interface.Here we have to define our interface name. For example:"VendorName""ModuleName""SomeFolderName""SomeClassInterface"And implementation.SomeClassName.Now we can use the object manager. The Magento uses the ObjectManager to generate and inject the classes declared in your constructor. You should avoid using ObjectManager directly in your code, like in this example, because it hides the real dependencies of the class.For testing purposes, let's add an object manager interface to our controller.Let's remove the unnecessary qualifier.Let's add some parameter to the class for this variable.And again, we don't have to call the object manager directly like we do now because the framework handles that automatically.Let's disable the cache.Let's try using the object manager to initialize our class instead of the full path.Also, we need to clean up the generated folder as Magento uses its code compiler tool to collect all the class dependency information and creates auto-generated classes based on this information here.Let's check.This method name.It works fine, but now let's remove the object manager from the code and try using the constructor.Let's add our interface to the constructor.And add a class parameter for this variable.Let's check, but don't forget to clear the cache or disable it and also clear the generated folder every time you add something to the constructor or make any changes to the .xml files.Now let's create another class that implements the same interface.And let's return a different valueAnd let's change our implementation in "dependency injection" DI dot xml file from someClassName to AnotherOneClassNameDon't forget to clear the cache, delete the generated folder and let's check.As you can see, we have not changed either the method name or the interface name. The only thing we did was implementation in the di.xml file.This allows for loose code coupling because our controller no longer has to worry about initializing its own dependencies.Our custom class decides which implementations to provide to the controller, depending on the configuration or desired behavior.This is an important concept for Magento developers to understand, as it forms the basis of how Magento composes its classes.Here's how dependency injection actually works in Magento and what an object manager is.
You have completed 0% of the lesson
0%