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.

bowlerae
  • 924
  • 1
  • 14
  • 37
  • 3
    Put `use Cake\Core\Configure;` in your layout file. For a reason why see this questions http://stackoverflow.com/questions/5667796/do-namespace-declarations-within-included-files-automatically-include-the-parent – ankr Oct 30 '15 at 19:51
  • @ankr ...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. – bowlerae Oct 30 '15 at 20:15
  • 1
    If it is being the SettingsSite.title is being used on every page, why not set it as a view variable in app controller? – chrisShick Oct 30 '15 at 21:54
  • 2
    That's simply how PHP namespace imports work, it's a per-file feature. – ndm Oct 30 '15 at 23:08
  • 1
    Cake 3 use namespaces and Cake 2 didn't, and as @ndm 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`. – ankr Oct 31 '15 at 00:09
  • 1
    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. – ankr Oct 31 '15 at 00:11
  • 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. – bowlerae Nov 02 '15 at 14:43
  • Sounds like a better solution @bowlerae (Y) Good luck! – ankr Nov 02 '15 at 15:24
  • @chrisShick this is exactly what I did. If you want to put that in an answer I can mark it as correct. – bowlerae Nov 04 '15 at 13:56

3 Answers3

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);
Fury
  • 4,643
  • 5
  • 50
  • 80
3

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

chrisShick
  • 1,096
  • 8
  • 21
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>
Amuk Saxena
  • 1,521
  • 4
  • 18
  • 44
  • 1
    Or just use a helper wrapper for it :) [Shim.Configure helper](https://github.com/dereuromark/cakephp-shim/blob/master/src/View/Helper/ConfigureHelper.php) - no need for use statements then. – mark Mar 27 '17 at 14:48