Setting up a controller for ACF fields

Thanks, creating that class works now. I still don’t really get it though because regardless of what namespace I use in the controller - App\Controllers or App or even Controllers, they all work! The only thing that doesn’t work is if I remove the namespace from the controller altogether.

What if I want to use the same controller on a different view, say a different archive or single view for other post types, how would I go about that?

Appreciated the help greatly!

Without knowing which version of Controller you’re using, it’s difficult to answer your question about namespacing. I would stick with App\Controllers because that’s what @withjacoby has in the Controller readme, and it seems most inline with proper namespacing for Sage.

In general controllers should be unique to views. If you want to have a generic “Archive” controller, then you could change your class name and file name to Archive. Otherwise I’d recommend just creating a new controller. Controllers are matched like Blades using the content of the body class that WordPress generates, so in theory you can create a controller that matches any class in that filter. Just remember that Controller is using PSR-4 to load controllers, so your file names and class names need to match, and need to conform to PSR-4 standards (which mostly means they need to be CamelCase—i.e. page-template-contact would be PageTemplateContact).

Also: Updated versions of Controller now support ACF fields natively.

OK I think I’m startng to get it! With the support for ACF fields, I added…

protected $acf = true;

…to the controller, so according to the readme that should pass on all of the acf field data to the view. But how do I actually access that on the view? Say I have a field short_description should I ge able to just use


on my blade template? I did just try this and I’m getting an undefined variable error. Maybe I need to update Controller? How to I tell which version I have?


As far as I can see my Controller version is 9.0.0-beta.4 - does that make sense?

You can update your Controller version by changing the version value in your composer.json. Yours probably looks like this right now:

"soberwp/controller": "9.0.0-beta.4"

I believe that the ACF functionality was added in 2.0.0, so you could change it to:

"soberwp/controller": "^2.0.0"

Then run composer update.


Hmm… tried that but now I’m getting…

Fatal error: Uncaught ReflectionException: Class App\Controllers\app does not exist in /Users/kevin.price-ward/Sites/jewson_bbc/web/app/themes/bbc/vendor/soberwp/controller/src/Loader.php on line 119

Any ideas?

Class App\Controllers\app makes it look like you aren’t naming your classes correctly. Specifically, this suggests that you used lower-case for your class name. You need to follow the PSR-4 standard. In this particular case, your App.php file should look like this:


namespace App\Controllers;

use Sober\Controller\Controller;

class App extends Controller
   // Whatever stuff you want to do in this controller...

Brilliant… thanks @alwaysblank, I had to change the namespace and rename the file on both app.php and front-page.php but working now!

Glad it worked for you. :slight_smile: If you can, please mark the post that solved it for you as the solution.


I’ve been following this thread and hope it’s okay to add my problem in here.

I’ve updated my Controller version from 9.0.0-beta.4 to 2.0.0. The namespaces of my controllers were App so I’ve changed these to App\Controllers. I’m still getting the fatal error:

Fatal error: Uncaught ReflectionException: Class App\Controllers\front-page does not exist in /srv/www/ on line 119

All I have in the controller front-page.php is:

namespace App\Controllers;

use Sober\Controller\Controller;

class FrontPage extends Controller

Can anyone help? I feel really clueless about this.

The filename of the file containing your front page class needs to have the same name as the class; in this case, your file should be FrontPage.php. Controller uses the PSR-4 spec to autoloader files, which determines how files should be named and organized. You can read the details here:

I renamed the files to App.php and FrontPage.php, changed line 3 to be ‘namespace App\Controllers;’, and I’m still getting a ReflectionException error:

Fatal error: Uncaught ReflectionException: Class App\Controllers\App does not exist in /srv/www/.../vendor/soberwp/controller/src/Loader.php on line 119

You’ll need to provide some more information about your setup for us to try and help. What version of Controller are you using? What is the rest of your code in App.php?

Controller is ^2.0.0. App.php is just the default file that ships with Sage, it just has a siteName and title method. All I did was change line three. The folder structure is /app/controllers/App.php FrontPage.php. This is a Trellis/Bedrock/Sage site, and I really haven’t touched anything other than the composer.json file. The Call Stack says it fails during the __contruct() in the Loader.php file.

Thanks @alwaysblank. I’m getting closer to getting it working now. I’ve renamed:
app.php to App.php to FrontPage.php
page.php to Page.php
I’ve taken all the code I added out of the above pages so there’s nothing there to sully them.

I can now see my dashboard which is definite progress. However when I try to look at the site I get an error to do with name spacing. I’ve been debugging the site having followed A Sage 9 + ACF debugging technique

I’ve now got two errors:
Symfony\Component\Debug\Exception\FatalThrowableError Class 'Sober\Controller\Module\Debugger' not found

Class 'Sober\Controller\Module\Debugger' not found (View: /srv/www/

page-header.blade.php contains:

if ($_SERVER['WP_ENV'] == 'development') {
  $controller = new \Sober\Controller\Module\Debugger(get_defined_vars(), 'Debugger');
  $fields = get_fields();

I really am trying to work it out for myself but I really need help with this bit to help me understand it all.

It looks like the namespace for the debugger has changed to Sober\Controller\Debugger as per the source found here:

Errors about “Class ‘SomeClass’ not found” or about “ReflectionError” frequently mean that the namespace or class name is wrong either where a class is being instantiated, or in the original definition of that class. If you’re struggling with namespaces, this post I wrote attempting to explain how they work may be helpful: Controller namespace issue, different behavior on localhost and development server

Thank you again @alwaysblank

I’d even looked in the debugger to check the namespace but I think I’m panicking too much to think properly at the moment. My understanding is very tentative but I’m getting there. Your help has been invaluable.

Just got to figure out the ACF bit now …

1 Like

I think I’ve solved this issue:

1 Like