Yii2簡單紀要

2022-07-29 09:42:15 字數 1079 閱讀 5980

網上經常拿yii來模擬ror,從mvc角度,使用體驗及**風格上確實有很多相似的地方。不過看配置檔案發現yii2不止是受rails的影響,同樣有不少spring的影子,最明顯的就是配置檔案中很多ioc和aop的體現。yii和spring都實現了乙個ioc容器來管理物件依賴關係及其生命週期。yii2使用了service locator(ioc的一種實現)與di,通過config/web.php檔案配置。spring則需要配置xml檔案,其中aop配置可通過xml檔案或者**註解(需要依賴aop框架)實現,和hibernate類似 。

ioc是很好的設計模式,程式可方便接偶,具體參照經典

ioc 容器和 dependency injection 模式

aop的實現機制

我對aop的理解

spring aop 實現原理

ci通過下面新增鉤子函式也能實現aop,不過不如yii原生支援得好

#

config/config.php

$config['enable_hooks'] = true;

$config['acl_pass'] = array

( 'testcontroller' => array('index'=>true,'getsecret'=>false),

'welcome' => array('login'=>true));

#config/hook.php

$hook['pre_controller'] = array

( 'class' => 'my_acl',

'function' => 'hook_acl',

'filename'=> 'acl.php',

'filepath' => 'hooks',

'params' => array(),);

#hooks/acl.php

<?php if ( ! defined('basepath')) exit('no direct script access allowed');

class my_acl extends

ci_controller}}

}

yii2的簡單安裝

每到乙個公司都有自己的php框架,目前沒有和開源完全重合的。一直想完整的讀乙個開源的php框架的 開源框架太多,最近選取yii作為分析物件 1 安裝composer 我使用的是brew安裝的 create project建立乙個專案 prefer dist 當有可用的包時,從 dist 安裝。sta...

Yii2 設計模式 簡單工廠模式

除了使用 new 操作符之外,還有更多的製造物件的方法。你將了解到例項化這個活動不應該總是公開進行,也會認識到初始化經常造成 耦合 問題。yii db mysql schema 中 建立 querybuilder 例項 public function createquerybuilder 這裡使用了...

yii2框架 yii2自身的自動載入 三

上一節說完了composer的自動載入,下面我們來說一下yii2自身的自動載入。在我們的入口檔案,例如index.php,如下 comment out the following two lines when deployed to production defined yii debug or d...