Home  >  Article  >  Backend Development  >  Detailed explanation of how to add custom classes to IoC container in Laravel5.4

Detailed explanation of how to add custom classes to IoC container in Laravel5.4

*文
*文Original
2018-01-03 15:31:531528browse

Laravel is a framework that is easy to use but not easy to understand. Why not simple? Because it contains a lot of so-called "advanced" concepts, among which dependency injection (DI) and Ioc containers are one of the core contents. This article mainly introduces you to the relevant information about adding custom classes to the IoC container in Laravel 5.4. Friends in need can refer to it. I hope to be helpful.

IoC Container

Container, literally means a thing that holds things. Common variables, object properties, etc. can be regarded as containers. What a container can hold depends entirely on how you define the container. Of course, there is such a container, which does not store text or values, but objects, object descriptions (classes, interfaces) or provides callbacks for objects. Through this container, we can implement many advanced functions, the most important of which is What is often mentioned is "decoupling" and "dependency injection (DI)".

IoC container - the core of Laravel

The core of Laravel is an IoC container. According to the documentation, it is called a "service container". As the name suggests, this container provides A range of services are required. As beginners, many people will have trouble with this concept. Therefore, I plan to start with some basic content and gradually uncover the veil of "dependency injection" by understanding the generation and solution of dependencies in object-oriented development. Gradually understand this magical design concept.

This article will introduce in detail the relevant content about adding custom classes to the IoC container in Laravel5.4, and share it for your reference and study. I won’t say much below, let’s take a look at the detailed introduction.

IoC container adds custom class

1. Suppose we want to build a transaction class, app/Trade/Sale.php

<?php
namespace App\Trade;
class Sale
{
public function exchange()
{
 dd(&#39;交易成功啦!&#39;);
}
}

2. Create a service provider TradeServiceProvider

php artisan make:provider TradeServiceProvider

3 in the Providers directory, and then bind our class in the register method of TradeServiceProvider.php

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
 //
 $this->app->bind(&#39;trade&#39;,function(){
  return new Sale();
 });
}

4. Next, add a line to the providers array under config/app.php,

App\Providers\TradeServiceProvider::class,

5, and then write a test in our routing

Route::get(&#39;/&#39;,function (){
$trade = app(&#39;trade&#39;);
dd($trade->exchange());
}

Output result: Transaction successful!

6. At this point we have added our own classes to the IoC container

Related recommendations:

Some practical tips to improve the performance of Laravel 5

Laravel Redis multiple processes taking the queue problem at the same time

laravel writing APP interface (API)

The above is the detailed content of Detailed explanation of how to add custom classes to IoC container in Laravel5.4. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn