應用元件

程式碼修行者發表於2015-02-07

應用元件

在YII開始載入的時候
首先例項化了CWebApplication類
CWebApplication extends CApplication
CApplication類的構造方法中 註冊了應用元件

$components=array(
            'coreMessages'=>array(
                'class'=>'CPhpMessageSource',
                'language'=>'en_us',
                'basePath'=>YII_PATH.DIRECTORY_SEPARATOR.'messages',
            ),
            'db'=>array(
                'class'=>'CDbConnection',
            ),
            'messages'=>array(
                'class'=>'CPhpMessageSource',
            ),
            'errorHandler'=>array(
                'class'=>'CErrorHandler',
            ),
            'securityManager'=>array(
                'class'=>'CSecurityManager',
            ),
            'statePersister'=>array(
                'class'=>'CStatePersister',
            ),
            'urlManager'=>array(
                'class'=>'CUrlManager',
            ),
            'request'=>array(
                'class'=>'CHttpRequest',
            ),
            'format'=>array(
                'class'=>'CFormatter',
            ),
        );

1.使用舉例

browser使用的前提是要下載 browscap.ini
並且在 php.ini中指定
[browscap]
browscap = “路徑/browscap.ini”
不過這樣檔案有13M 執行起來有1秒鐘左右所以僅供測試

class IndexController extends CController{
    public function actionIndex()
    {
        //request 為CHttpRequest的別名
        var_dump(Yii::app()->request->browser);
    }
}

結果

array (size=30)
  'browser_name_regex' => string '§^mozilla/5\.0 \(.*windows nt 6\.1.*wow64.*\) applewebkit/.* \(khtml, like gecko\) chrome/39\..*safari/.*$§' (length=107)
  'browser_name_pattern' => string 'Mozilla/5.0 (*Windows NT 6.1*WOW64*) AppleWebKit/* (KHTML, like Gecko) Chrome/39.*Safari/*' (length=90)
  'parent' => string 'Chrome 39.0' (length=11)
  'platform' => string 'Win7' (length=4)
  'platform_version' => string '6.1' (length=3)
  'win32' => string '' (length=0)
  'win64' => string '1' (length=1)
  'comment' => string 'Chrome 39.0' (length=11)
  'browser' => string 'Chrome' (length=6)
  'version' => string '39.0' (length=4)
  'majorver' => string '39' (length=2)
  'frames' => string '1' (length=1)
  'iframes' => string '1' (length=1)
  'tables' => string '1' (length=1)
  'cookies' => string '1' (length=1)
  'javascript' => string '1' (length=1)
  'javaapplets' => string '1' (length=1)
  'cssversion' => string '3' (length=1)
  'minorver' => string '0' (length=1)
  'alpha' => string '' (length=0)
  'beta' => string '' (length=0)
  'win16' => string '' (length=0)
  'backgroundsounds' => string '' (length=0)
  'vbscript' => string '' (length=0)
  'activexcontrols' => string '' (length=0)
  'ismobiledevice' => string '' (length=0)
  'istablet' => string '' (length=0)
  'issyndicationreader' => string '' (length=0)
  'crawler' => string '' (length=0)
  'aolversion' => string '0' (length=1)

相關文章