Skip to content

Commit f196b16

Browse files
author
Adegoke Obasa
committed
(Dependency Injection): Implement dependency injection sample in Yii2
1 parent c6ffc1d commit f196b16

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

app/config/services.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/**
3+
* @author Adegoke Obasa <[email protected]>
4+
*/
5+
Yii::$container->set('app\services\DummyServiceInterface', 'app\services\DummyService');

app/controllers/SiteController.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
namespace app\controllers;
44

5+
use app\models\ContactForm;
6+
use app\models\LoginForm;
7+
use app\services\DummyServiceInterface;
58
use Yii;
69
use yii\filters\AccessControl;
710
use yii\filters\VerbFilter;
8-
use app\models\LoginForm;
9-
use app\models\ContactForm;
1011

1112
class SiteController extends BaseController
1213
{
14+
/** @var DummyServiceInterface */
15+
protected $dummyService;
16+
17+
public function __construct($id, $module, DummyServiceInterface $dummyService, $config = [])
18+
{
19+
$this->dummyService = $dummyService;
20+
parent::__construct($id, $module, $config);
21+
}
22+
1323
public function behaviors()
1424
{
1525
return [
@@ -51,6 +61,11 @@ public function actionIndex()
5161
return $this->render('index');
5262
}
5363

64+
public function actionServiceTest()
65+
{
66+
return $this->dummyService->shout("Hello World");
67+
}
68+
5469
public function actionLogin()
5570
{
5671
if (!\Yii::$app->user->isGuest) {
@@ -88,8 +103,9 @@ public function actionContact()
88103

89104
public function actionAbout()
90105
{
91-
if($this->getUser()->isGuest)
106+
if ($this->getUser()->isGuest) {
92107
return $this->getUser()->loginRequired();
108+
}
93109
return $this->render('about');
94110
}
95111
}

app/services/DummyService.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @author Adegoke Obasa <[email protected]>
4+
*/
5+
6+
namespace app\services;
7+
8+
use yii\base\Object;
9+
10+
class DummyService extends Object implements DummyServiceInterface
11+
{
12+
public function shout($text)
13+
{
14+
return $text;
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* @author Adegoke Obasa <[email protected]>
4+
*/
5+
6+
namespace app\services;
7+
8+
interface DummyServiceInterface
9+
{
10+
public function shout($text);
11+
}

app/web/index.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
$envs = ['development', 'staging', 'production'];
1818
$env = getenv("APPLICATION_ENV");
1919

20-
if(!in_array($env, $envs)) {
20+
if (!in_array($env, $envs)) {
2121
die("Environment is not valid");
2222
}
2323

2424
// Only show debug toolbar and allow gii when in development
25-
if($env == "development") {
25+
if ($env == "development") {
2626
defined('YII_DEBUG') or define('YII_DEBUG', true);
2727
defined('YII_ENV') or define('YII_ENV', 'dev');
2828
}
@@ -32,6 +32,10 @@
3232
// Require configuration file
3333
$config = require(__DIR__ . "/../config/web.php");
3434

35+
36+
// Service Dependencies
37+
require(__DIR__ . "/../config/services.php");
38+
3539
// Change this to your timezone
36-
ini_set( 'date.timezone', 'Africa/Lagos');
37-
(new yii\web\Application($config))->run();
40+
ini_set('date.timezone', 'Africa/Lagos');
41+
(new yii\web\Application($config))->run();

0 commit comments

Comments
 (0)