CakePHP 3:未找到“配置”类

问题描述 投票:5回答:3

我昨天刚开始使用CakePHP v3.1.3,而且我一次只能在我的CakePHP 2.4.2网站上迁移一小部分。主要是试图了解变化。我和Configure::read()有问题。

一点背景信息:我的数据库中有SettingsSite,SettingsSiteHeaders和SettingsSitesOptions表,我也为这些表做了相应的表对象。在我的SettingsSiteTable类中,我有一个方法可以检索存储在数据库中的设置。一切都很好,以下代码在我的AppController中工作没问题。

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Core\Configure;

class AppController extends Controller{

    public function beforeFilter(Event $event){
        debug(Configure::read('SettingsSite.title'));
    }

我的问题是在我的视图中正确显示值。

我在主题默认布局中有以下代码,位于plugins / Default / src / Template / Layout / default.ctp中

<title>
    <?php
        echo $this->fetch('title') . ' | ' .
        Configure::read('SettingsSite.title');
    ?>
</title>

我收到一个“类'配置'没有找到插件/ Default / src / Template / Layout / default.ctp”错误消息。

然后我转到AppView.php文件并在命名空间声明下添加以下行:

use Cake\Core\Configure;

错误仍然存​​在。

cakephp cakephp-3.0 cakephp-3.1
3个回答
3
投票

只需在app控制器的beforeFilter中将您的设置变为视图变量:)


7
投票

只是用

use Cake\Core\Configure;

在你的控制器上

此外,如果您在app文件夹中有自定义配置文件。您可以在Bootstrap文件中加载它并像这样加载Config;

$config = Configure::read('App.namespace');
debug($config);

2
投票

在您的布局上使用以下行。这对我有用:

<?php use Cake\Core\Configure; ?>

<title>
    <?php
        echo $this->fetch('title') . ' | ' .
        Configure::read('SettingsSite.title');
    ?>
</title>
© www.soinside.com 2019 - 2024. All rights reserved.