当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii2框架配置文件(Application属性)与调试技巧实例分析

Yii2框架配置文件(Application属性)与调试技巧实例分析

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了yii2框架配置文件(application属性)与调试技巧。分享给大家供大家参考,具体如下:

配置文件

yii2的主要配置文件config\web.php:

<?php
$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' => 'aldjaldjaldjaljd',
    ],
    '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'],
        ],
      ],
    ],
    'db' => require(__dir__ . '/db.php'),
    'urlmanager' => [
      'enableprettyurl' => true,
      'showscriptname' => false,
      'rules' => [
      ],
    ],
  ],
  '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;

最后返回的一个数组,数组的key都是application的属性。

我们到控制器中来访问一下:

public function actionindex()
{
    echo \yii::$app->id,'<br>';
    echo \yii::$app->name,'<br>';
    exit;
    return $this->render('index',['username'=>'张三','age'=>22]);
}

这里写图片描述

在入口文件web/index.php 里会加载这个config.php 配置文件,来创建一个application

#...
$config = require(__dir__ . '/../config/web.php');
(new yii\web\application($config))->run();

调试技巧

助手类yii,服务于整个框架,提供一些基础方法:记录日志、调试等
\yii:warning()日志文件runtime/logs/app.log
\yii::error()
\yii::info()

\yii:trace('调试内容','test') 

这里写图片描述

这里写图片描述

更多关于yii相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网