mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
di doc WIP [skip ci]
This commit is contained in:
@ -4,5 +4,6 @@ To add new guide, take the following steps:
|
||||
|
||||
1. Create `guide-name` and put there relevant documentation;
|
||||
2. If guide has more then one word in name, then it should be with dashes, like: `console-fixture.md`, `module-debug.md`;
|
||||
3. If your guide is for console commands, than its name should follow convention: `console-{command}.md`;
|
||||
4. If your guide is for custom modules, than its name should follow convention: `module-{moduleName}.md`.
|
||||
3. If your guide is about console commands, than its name should follow convention: `console-{command}.md`;
|
||||
4. If your guide is about module usages, than its name should follow convention: `module-{moduleName}.md`.
|
||||
5. If your guide is about widget usages, than its name should follow convention: `widget-{moduleName}.md`.
|
||||
|
31
docs/guide/di.md
Normal file
31
docs/guide/di.md
Normal file
@ -0,0 +1,31 @@
|
||||
Service Locator and Dependency Injection
|
||||
========================================
|
||||
|
||||
Both service locator and dependency injection are design patterns that allow building software
|
||||
in a loosely-coupled fashion. Yii uses service locator and dependency injection extensively,
|
||||
even though you may not be aware of them. In this tutorial, we will explore their implementation
|
||||
and support in Yii to help you write code more consciously. We also highly recommend you to
|
||||
read [Martin's article](http://martinfowler.com/articles/injection.html) to get a deeper
|
||||
understanding of SL and DI.
|
||||
|
||||
|
||||
Service Locator
|
||||
---------------
|
||||
|
||||
A service locator is an object that knows how to provide all sorts of services that an application
|
||||
might need. The most commonly used service locator in Yii is the *application* object accessible through
|
||||
`\Yii::$app`. It provides services under the name of *application components*. The following code
|
||||
shows how you can obtain an application component (service) from the application object:
|
||||
|
||||
```php
|
||||
$request = \Yii::$app->get('request');
|
||||
// or alternatively
|
||||
$request = \Yii::$app->request;
|
||||
```
|
||||
|
||||
Behind the scene, the application object serves as a service locator because it extends from
|
||||
the [[yii\di\ServiceLocator]] class.
|
||||
|
||||
|
||||
Dependency Injection
|
||||
--------------------
|
@ -36,6 +36,7 @@ Base concepts
|
||||
- [Event Handling](events.md) - The Yii event handling mechanism
|
||||
- [Behaviors](behaviors.md)
|
||||
|
||||
|
||||
Database
|
||||
--------
|
||||
|
||||
@ -90,6 +91,7 @@ Advanced Topics
|
||||
- [Console Application](console.md)
|
||||
- [Performance Tuning](performance.md)
|
||||
- [Testing](testing.md)
|
||||
- [Service Locator and Dependency Injection](di.md)
|
||||
|
||||
References
|
||||
----------
|
||||
|
Reference in New Issue
Block a user