-
類級別的事件綁定查看全部
-
模塊化重新整理查看全部
-
調(diào)用子模塊查看全部
-
擴展性:模塊化技術、事件機制、mixin、依賴注入查看全部
-
事件機制查看全部
-
圖片信息查看全部
-
modules查看全部
-
依賴注入的服務定位器是建立在容器之上的查看全部
-
另一個版本: <?php namespace app\controllers; use Yii; use yii\web\Controller; class YlzrController extends Controller{ public function actionIndex(){ \yii::$container->set('app\controllers\Driver', 'app\controllers\ManDriver') ; /* 版本1:需要先在app\config\web.php中的components項下注冊 'car'=> [ 'class' => 'app\controllers\Car' ], */ \yii::$app->car->run(); } } interface Driver{ public function drive(); } class ManDriver implements Driver{ public function drive(){ echo 'i am an old man!'; } } class Car{ private $driver = null; public function __construct(Driver $driver){ $this->driver = $driver; } public function run(){ $this->driver->drive(); } }查看全部
-
版本1: <?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\di\ServiceLocator; class YlzrController extends Controller{ public function actionIndex(){ \yii::$container->set('app\controllers\Driver', 'app\controllers\ManDriver') ; $sl = new ServiceLocator; $sl -> set('car',[ 'class' => 'app\controllers\Car' ]); $car = $sl->get('car'); $car->run(); } } interface Driver{ public function drive(); } class ManDriver implements Driver{ public function drive(){ echo 'i am an old man!'; } } class Car{ private $driver = null; public function __construct(Driver $driver){ $this->driver = $driver; } public function run(){ $this->driver->drive(); } }查看全部
-
依賴注入: 1、容器:定義并解決依賴關系; 2、服務定位器:配置服務的參數(shù)信息查看全部
-
引入接口繼續(xù)消除耦合: <?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\di\Container; class YlzrController extends Controller{ public function actionIndex(){ $container = new Container; $container->set('app\controllers\Driver', 'app\controllers\ManDriver'); $car = $container -> get('app\controllers\Car'); $car->run(); } } interface Driver{ public function drive(); } class ManDriver implements Driver{ public function drive(){ echo 'i am an old man!'; } } class Car{ private $driver = null; public function __construct(Driver $driver){ $this->driver = $driver; } public function run(){ $this->driver->drive(); } }查看全部
-
PHP中接口的使用 interface Driver{ public function drive(); } class ManDriver implements Driver{ public function drive(){ echo 'i am an old man!'; } }查看全部
-
不完美版(未用到接口): <?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\di\Container; class YlzrController extends Controller{ public function actionIndex(){ $container = new Container; $car = $container -> get('app\controllers\Car'); $car->run(); } } class ManDriver{ public function drive(){ echo 'i am an old man!'; } } class Car{ private $driver = null; public function __construct(ManDriver $driver){ $this->driver = $driver; } public function run(){ $this->driver->drive(); } }查看全部
-
1、可以利用Gii方式簡單的配置一個子模塊; 2、將新創(chuàng)建的模塊在系統(tǒng)中注冊,如添加到config/web.php中,具體添加代碼在gii中生成器里面有提示。 3、生成的子模塊實際上就是MVC架構的。 4、如何調(diào)用子模塊: 1、在父模塊的控制器中調(diào)用子模塊,如調(diào)用article中的DefaultController控制器中的actionIndex方法 <?php namespace app\controllers; use Yii; use yii\web\Controller; class HelloController extends Controller { public function actionIndex() { //調(diào)用子模塊 $article = \yii::$app->getModule('article'); //調(diào)用子模塊的操作 $article->runAction('default/index'); } } 2、直接在瀏覽器中通過路由方式調(diào)用新創(chuàng)建的模塊,如:http://yiibasic.dev/index.php?r=article/default/index查看全部
舉報
0/150
提交
取消