-
类级别的事件绑定查看全部
-
模块化重新整理查看全部
-
调用子模块查看全部
-
扩展性:模块化技术、事件机制、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、服务定位器:配置服务的参数信息查看全部
-
引入接口继续消除耦合: <?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、将新创建的模块在系统中注册,如添加到config/web.php中,具体添加代码在gii中生成器里面有提示。 3、生成的子模块实际上就是MVC架构的。 4、如何调用子模块: 1、在父模块的控制器中调用子模块,如调用article中的DefaultController控制器中的actionIndex方法 <?php namespace app\controllers; use Yii; use yii\web\Controller; class HelloController extends Controller { public function actionIndex() { //调用子模块 $article = \yii::$app->getModule('article'); //调用子模块的操作 $article->runAction('default/index'); } } 2、直接在浏览器中通过路由方式调用新创建的模块,如:http://yiibasic.dev/index.php?r=article/default/index查看全部
举报
0/150
提交
取消