php yii配置,Yii配置

以下是数据库配置的一个例子(在之前的例子中我们已经接触了) -

$config = [

'class' => '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,

]

一个基本的应用程序模板的配置文件是组成复杂配置的一个小部分 -

$params = require(__DIR__ . '/params.php');

$config = [

'id' => '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 文件中定义它了 -

//defining global constans

defined('YII_DEBUG') or define('YII_DEBUG', true);

defined('YII_ENV') or define('YII_ENV', 'dev');

//register composer autoloader

require(__DIR__ . '/../vendor/autoload.php');

//include yii files

require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

//load application config

$config = require(__DIR__ . '/../config/web.php');

//create, config, and process request

(new yii\web\Application($config))->run();

?>

许多小部件也使用配置,如下面的代码中所示。

NavBar::begin([

'brandLabel' => '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 配置文件就知道了 -

Yii::setAlias('@tests', dirname(__DIR__) . '/tests');

$params = require(__DIR__ . '/params.php');

$db = require(__DIR__ . '/db.php');

return [

'id' => '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,

]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值