溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Yii2框架配置文件與調(diào)試技巧的示例分析

發(fā)布時(shí)間:2021-08-05 14:33:12 來(lái)源:億速云 閱讀:135 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Yii2框架配置文件與調(diào)試技巧的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

具體如下:

配置文件

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;

最后返回的一個(gè)數(shù)組,數(shù)組的key都是Application的屬性。

我們到控制器中來(lái)訪問(wèn)一下:

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

Yii2框架配置文件與調(diào)試技巧的示例分析

在入口文件web/index.php 里會(huì)加載這個(gè)config.php 配置文件,來(lái)創(chuàng)建一個(gè)Application

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

調(diào)試技巧

助手類Yii,服務(wù)于整個(gè)框架,提供一些基礎(chǔ)方法:記錄日志、調(diào)試等
\Yii:warning()日志文件runtime/logs/app.log
\Yii::error()
\Yii::info()

\Yii:trace('調(diào)試內(nèi)容','test') 

Yii2框架配置文件與調(diào)試技巧的示例分析

Yii2框架配置文件與調(diào)試技巧的示例分析

感謝各位的閱讀!關(guān)于“Yii2框架配置文件與調(diào)試技巧的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI