CakePHP 3: Class 'Configure' Not Found
Asked Answered
P

3

5

I just started with CakePHP v3.1.3 yesterday and I'm migrating teeny tiny bits of my CakePHP 2.4.2 site over at a time. Mostly just trying to get an understanding of the changes. I'm having an issue with Configure::read().

A little background info: I have SettingsSite, SettingsSiteHeaders, and SettingsSitesOptions tables in my db, I also made corresponding table objects for those. In my SettingsSiteTable class I have a method that retrieves the settings stored in the database. All is well and the following code works in my AppController no problem.

<?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'));
    }

I'm having an issue getting the value to display properly in my view.

I have the following code in my theme default layout located in plugins/Default/src/Template/Layout/default.ctp

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

I was receiving a "Class 'Configure' not found in plugins/Default/src/Template/Layout/default.ctp" error message.

So I then went to the AppView.php file and added the following line under the namespace declaration:

use Cake\Core\Configure;

Error is still there.

Paramedic answered 30/10, 2015 at 19:22 Comment(9)
Put use Cake\Core\Configure; in your layout file. For a reason why see this questions #5668296Wickner
@Wickner ...that kind of sucks to have to do. The cook book says "You can access Configure from anywhere in your application: Configure::read('debug');" And we used to not have to do this in version 2. Also, I tried placing it in my default.ctp layout file thinking it would apply to every view that uses the template but it doesn't. It's very inconvenient to have to put this inside of EVERY view file. There has to be a better way.Paramedic
If it is being the SettingsSite.title is being used on every page, why not set it as a view variable in app controller?Chow
That's simply how PHP namespace imports work, it's a per-file feature.Samellasameness
Cake 3 use namespaces and Cake 2 didn't, and as @Samellasameness mentions this is behavior of PHP rather than Cake. An option is to use a Helper to get the information you want - if nothing else then just as a proxy for Configure.Wickner
But the fact that you use Configure in "every file" sounds like there might be better options, such as sending the data from the controller or using a helper as mentioned above.Wickner
Thanks everyone. In my SettingsSiteTable object I decided to return the array of settings instead of saving them via Configure::write(). I am then able to store the results in a view variable.Paramedic
Sounds like a better solution @Paramedic (Y) Good luck!Wickner
@Chow this is exactly what I did. If you want to put that in an answer I can mark it as correct.Paramedic
C
3

Just simply make your Settings a view variable in the app controller's beforeFilter :)

Chow answered 4/11, 2015 at 15:37 Comment(1)
Never beforeFilter, beforeRender is the correct one IMO.Homesteader
S
7

Just use

use Cake\Core\Configure;

on top of your controller

Also If you have custom config file in app folder. You can load it in Bootstrap file and load the Config like this;

$config = Configure::read('App.namespace');
debug($config);
Sprang answered 21/2, 2016 at 21:14 Comment(0)
C
3

Just simply make your Settings a view variable in the app controller's beforeFilter :)

Chow answered 4/11, 2015 at 15:37 Comment(1)
Never beforeFilter, beforeRender is the correct one IMO.Homesteader
P
2

On your layout use the below line. This worked for me:

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

<title>
    <?php
        echo $this->fetch('title') . ' | ' .
        Configure::read('SettingsSite.title');
    ?>
</title>
Pyelonephritis answered 27/3, 2017 at 6:26 Comment(1)
Or just use a helper wrapper for it :) Shim.Configure helper - no need for use statements then.Homesteader

© 2022 - 2024 — McMap. All rights reserved.