2

我正在尝试在 Yii2 中集成一个主题。我已将主题放在基本文件夹内的主题文件夹中。在 config/web.php 文件中,我在 components 数组下插入了以下代码。

  <?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' => 'bnfhxfasdf7324675@$%fg',
        ],
        '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'),
        'view' => [
            'theme' => [
                'basePath' => '@app/themes/flat',
                'baseUrl' => '@web/themes/flat',
                'pathMap' => [
                    '@app/views' => '@app/themes/flat/views',
                ],
            ],
        ],
    ],
    '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;

但我得到了错误

Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: yii\web\Application::theme

在 Component.php 中

foreach ($this->_behaviors as $behavior) {
                if ($behavior->canGetProperty($name)) {
                    return $behavior->$name;
                }
            }
        }
        if (method_exists($this, 'set' . $name)) {
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
        } else {
            throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
        }
    }

下面是来自 app.log 文件的堆栈跟踪。

2015-08-14 04:22:20 [127.0.0.1][-][-][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\web\Application::theme' in D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\Component.php:143
Stack trace:
#0 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\di\ServiceLocator.php(73): yii\base\Component->__get('theme')
#1 D:\wamp\www\yii2crm\basic\themes\flat\views\layouts\main.php(14): yii\di\ServiceLocator->__get('theme')
#2 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\View.php(325): require('D:\wamp\www\yii...')
#3 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\View.php(247): yii\base\View->renderPhpFile('D:\wamp\www\yii...', Array)
#4 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\Controller.php(386): yii\base\View->renderFile('D:\wamp\www\yii...', Array, Object(app\controllers\SiteController))
#5 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\Controller.php(372): yii\base\Controller->renderContent('<div class="sit...')
#6 D:\wamp\www\yii2crm\basic\controllers\SiteController.php(52): yii\base\Controller->render('index')
#7 [internal function]: app\controllers\SiteController->actionIndex()
#8 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
#9 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\Controller.php(151): yii\base\InlineAction->runWithParams(Array)
#10 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('', Array)
#11 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('', Array)
#12 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#13 D:\wamp\www\yii2crm\basic\web\index.php(12): yii\base\Application->run()
#14 {main}
2015-08-14 04:22:20 [127.0.0.1][-][-][info][application] $_COOKIE = [
    '__utma' => '111872281.2069871515.1389088762.1438681554.1439275464.68'
    '__utmz' => '111872281.1389088762.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'
    'gpv_p23' => 'no value'
    's_nr' => '1392380191996-Repeat'
    'PHPSESSID' => 'pl7hrmbd2u5umjekfkc0ti0hq7'
]

$_SERVER = [
    'HTTP_HOST' => 'localhost'
    'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
    'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5'
    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
    'HTTP_COOKIE' => '__utma=111872281.2069871515.1389088762.1438681554.1439275464.68; __utmz=111872281.1389088762.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); gpv_p23=no%20value; s_nr=1392380191996-Repeat; PHPSESSID=pl7hrmbd2u5umjekfkc0ti0hq7'
    'HTTP_CONNECTION' => 'keep-alive'
    'HTTP_CACHE_CONTROL' => 'max-age=0'
    'PATH' => 'C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\VSShell\\Common7\\IDE\\;C:\\Program Files\\TortoiseSVN\\bin;D:\\wamp\\bin\\php\\php5.4.12\\;D:\\wamp\\www\\yii\\framework;D:\\wamp\\apps\\PostgreSQL\\9.3\\bin;C:\\Program Files (x86)\\EasyFrom Trial\\;C:\\Program Files\\nodejs\\;C:\\ProgramData\\ComposerSetup\\bin;'
    'SystemRoot' => 'C:\\Windows'
    'COMSPEC' => 'C:\\Windows\\system32\\cmd.exe'
    'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'
    'WINDIR' => 'C:\\Windows'
    'SERVER_SIGNATURE' => ''
    'SERVER_SOFTWARE' => 'Apache/2.4.4 (Win64) PHP/5.4.12'
    'SERVER_NAME' => 'localhost'
    'SERVER_ADDR' => '127.0.0.1'
    'SERVER_PORT' => '80'
    'REMOTE_ADDR' => '127.0.0.1'
    'DOCUMENT_ROOT' => 'D:/wamp/www'
    'REQUEST_SCHEME' => 'http'
    'CONTEXT_PREFIX' => ''
    'CONTEXT_DOCUMENT_ROOT' => 'D:/wamp/www'
    'SERVER_ADMIN' => 'admin@example.com'
    'SCRIPT_FILENAME' => 'D:/wamp/www/yii2crm/basic/web/index.php'
    'REMOTE_PORT' => '1554'
    'GATEWAY_INTERFACE' => 'CGI/1.1'
    'SERVER_PROTOCOL' => 'HTTP/1.1'
    'REQUEST_METHOD' => 'GET'
    'QUERY_STRING' => ''
    'REQUEST_URI' => '/yii2crm/basic/web/'
    'SCRIPT_NAME' => '/yii2crm/basic/web/index.php'
    'PHP_SELF' => '/yii2crm/basic/web/index.php'
    'REQUEST_TIME_FLOAT' => 1439526140.731
    'REQUEST_TIME' => 1439526140
]
2015-08-14 04:22:20 [127.0.0.1][-][-][warning][yii\log\Dispatcher::dispatch] Unable to send log via yii\debug\LogTarget: Exception 'Exception' with message 'Serialization of 'Closure' is not allowed' 

in D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2-debug\LogTarget.php:58

Stack trace:
#0 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2-debug\LogTarget.php(58): serialize(Array)
#1 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2-debug\LogTarget.php(112): yii\debug\LogTarget->export(Array)
#2 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\log\Dispatcher.php(183): yii\debug\LogTarget->collect(Array, true)
#3 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\log\Logger.php(173): yii\log\Dispatcher->dispatch(Array, true)
#4 D:\wamp\www\yii2crm\basic\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\log\Logger->flush(true)
#5 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\base\UnknownPropertyException))
#6 {main}
2015-08-14 04:22:20 [127.0.0.1][-][-][info][application] $_COOKIE = [
    '__utma' => '111872281.2069871515.1389088762.1438681554.1439275464.68'
    '__utmz' => '111872281.1389088762.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'
    'gpv_p23' => 'no value'
    's_nr' => '1392380191996-Repeat'
    'PHPSESSID' => 'pl7hrmbd2u5umjekfkc0ti0hq7'
]

$_SERVER = [
    'HTTP_HOST' => 'localhost'
    'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
    'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5'
    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
    'HTTP_COOKIE' => '__utma=111872281.2069871515.1389088762.1438681554.1439275464.68; __utmz=111872281.1389088762.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); gpv_p23=no%20value; s_nr=1392380191996-Repeat; PHPSESSID=pl7hrmbd2u5umjekfkc0ti0hq7'
    'HTTP_CONNECTION' => 'keep-alive'
    'HTTP_CACHE_CONTROL' => 'max-age=0'
    'PATH' => 'C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\VSShell\\Common7\\IDE\\;C:\\Program Files\\TortoiseSVN\\bin;D:\\wamp\\bin\\php\\php5.4.12\\;D:\\wamp\\www\\yii\\framework;D:\\wamp\\apps\\PostgreSQL\\9.3\\bin;C:\\Program Files (x86)\\EasyFrom Trial\\;C:\\Program Files\\nodejs\\;C:\\ProgramData\\ComposerSetup\\bin;'
    'SystemRoot' => 'C:\\Windows'
    'COMSPEC' => 'C:\\Windows\\system32\\cmd.exe'
    'PATHEXT' => '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'
    'WINDIR' => 'C:\\Windows'
    'SERVER_SIGNATURE' => ''
    'SERVER_SOFTWARE' => 'Apache/2.4.4 (Win64) PHP/5.4.12'
    'SERVER_NAME' => 'localhost'
    'SERVER_ADDR' => '127.0.0.1'
    'SERVER_PORT' => '80'
    'REMOTE_ADDR' => '127.0.0.1'
    'DOCUMENT_ROOT' => 'D:/wamp/www'
    'REQUEST_SCHEME' => 'http'
    'CONTEXT_PREFIX' => ''
    'CONTEXT_DOCUMENT_ROOT' => 'D:/wamp/www'
    'SERVER_ADMIN' => 'admin@example.com'
    'SCRIPT_FILENAME' => 'D:/wamp/www/yii2crm/basic/web/index.php'
    'REMOTE_PORT' => '1554'
    'GATEWAY_INTERFACE' => 'CGI/1.1'
    'SERVER_PROTOCOL' => 'HTTP/1.1'
    'REQUEST_METHOD' => 'GET'
    'QUERY_STRING' => ''
    'REQUEST_URI' => '/yii2crm/basic/web/'
    'SCRIPT_NAME' => '/yii2crm/basic/web/index.php'
    'PHP_SELF' => '/yii2crm/basic/web/index.php'
    'REQUEST_TIME_FLOAT' => 1439526140.731
    'REQUEST_TIME' => 1439526140
]

请帮忙。

4

2 回答 2

0

尝试使用以下命令更改您的配置文件:

'view' => [
         'theme' => [
             'pathMap' => [
                '@app/views' => '@app/themes/flat'
                ],
            ],
        ],
于 2015-08-14T10:38:34.380 回答
0

可能您在 yii1 样式中使用主题Yii::app()->theme,但在 yii2 中您可以[[yii\base\Theme]]通过属性访问对象[[yii\base\View::theme]]

于 2015-08-14T10:44:14.050 回答