Commit 29b9a92a by Qiang Xue

di doc WIP [skip ci]

parent 873cf243
......@@ -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`.
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
----------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment