Yii配置
配置用於創建新的對象或初始化現有的對象。構通常包括一個類名稱和初始值的列表。它們還可以包括事件處理程序和行爲的列表。
以下是數據庫配置的一個例子(在之前的例子中我們已經接觸了) -
'yii\\db\\Connection', 'dsn' => 'mysql:host = localhost;dbname = mystudy', 'username' => 'root', 'password' => '', 'charset' => 'utf8', \]; $db = Yii::createObject($config); ?>在 Yii::createObject() 方法需要一個配置數組,並基於配置類的名稱來創建對象。
配置的格式如下 -
[
//a fully qualified class name for the object being created
'class' => 'ClassName',
//initial values for the named property
'propertyName' => 'propertyValue',
//specifies what handlers should be attached to the object's events
'on eventName' => $eventHandler,
//specifies what behaviors should be attached to the object
'as behaviorName' => $behaviorConfig,
]
一個基本的應用程序模板的配置文件是組成複雜配置的一個小部分 -
'basic', 'basePath' => dirname(\_\_DIR\_\_), 'bootstrap' => \['log'\], 'components' => \[ 'request' => \[ // !!! insert a secret key in the following (if it is empty) - this //is required by cookie validation 'cookieValidationKey' => 'yfajskldfYiibai.com89dfad.dfasd#', \], 'cache' => \[ 'class' => 'yii\\caching\\FileCache', \], 'user' => \[ 'identityClass' => 'app\\models\\User', 'enableAutoLogin' => true, \], 'errorHandler' => \[ 'errorAction' => 'site/error', \], 'mailer' => \[ 'class' => 'yii\\swiftmailer\\Mailer', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, \], 'log' => \[ 'traceLevel' => YII\_DEBUG ? 3 : 0, 'targets' => \[ \[ 'class' => 'yii\\log\\FileTarget', 'levels' => \['error', 'warning'\], \], \], \], 'urlManager' => \[ //'showScriptName' => false, //'enablePrettyUrl' => true, //'enableStrictParsing' => true, //'suffix' => '/' \], 'db' => require(\_\_DIR\_\_ . '/db.php'), \], 'modules' => \[ 'admin' => \[ 'class' => 'app\\modules\\admin\\Admin', \], \], 'params' => $params, \]; if (YII\_ENV\_DEV) { // configuration adjustments for 'dev' environment $config\['bootstrap'\]\[\] = 'debug'; $config\['modules'\]\['debug'\] = \[ 'class' => 'yii\\debug\\Module', \]; $config\['bootstrap'\]\[\] = 'gii'; $config\['modules'\]\['gii'\] = \[ 'class' => 'yii\\gii\\Module', \]; } return $config; ?>在上面的配置文件,我們沒有定義類名。這是因爲我們已經在 index.php 文件中定義它了 -
run(); ?>許多小部件也使用配置,如下面的代碼中所示。
'My Company', 'brandUrl' => Yii::$app->homeUrl, 'options' => \[ 'class' => 'navbar-inverse navbar-fixed-top', \], \]); echo Nav::widget(\[ 'options' => \['class' => 'navbar-nav navbar-right'\], 'items' => \[ \['label' => 'Home', 'url' => \['/site/index'\]\], \['label' => 'About', 'url' => \['/site/about'\]\], \['label' => 'Contact', 'url' => \['/site/contact'\]\], Yii::$app->user->isGuest ? \['label' => 'Login', 'url' => \['/site/login'\]\] : \[ 'label' => 'Logout (' . Yii::$app->user->identity->username . ')', 'url' => \['/site/logout'\], 'linkOptions' => \['data-method' => 'post'\] \], \], \]); NavBar::end(); ?>當配置過於複雜,常見的做法是創建一個新的 PHP 文件,其中讓它返回一個數組。看看 config/console.php 配置文件就知道了 -
'basic-console', 'basePath' => dirname(\_\_DIR\_\_), 'bootstrap' => \['log', 'gii'\], 'controllerNamespace' => 'app\\commands', 'modules' => \[ 'gii' => 'yii\\gii\\Module', \], 'components' => \[ 'cache' => \[ 'class' => 'yii\\caching\\FileCache', \], 'log' => \[ 'targets' => \[ \[ 'class' => 'yii\\log\\FileTarget', 'levels' => \['error', 'warning'\], \], \], \], 'db' => $db, \], 'params' => $params, \]; ?>默認配置可以通過調用 Yii::$container->set() 方法來指定。通過 Yii::CreateObject()方法調用它可以爲應用創建默認配置指定的那些類的所有實例。
例如,自定義 yii\widgets\LinkPager 類,這樣所有鏈接頁面將顯示最多三個按鈕,使用如下代碼。
\Yii::$container->set('yii\widgets\LinkPager', [
'maxButtonCount' => 3,
]);