Fatal Uncaught ReflectionException error with Controller 2.0.0

Hi all,

I’m getting this error:

[08-Mar-2018 18:59:57 UTC] PHP Fatal error:  Uncaught ReflectionException: Class App\Controllers\FrontPage does not exist in /home/chapterserver1/dev/jewson_bbc/web/app/themes/bbc/vendor/soberwp/controller/src/Loader.php:119
Stack trace:
#0 /home/chapterserver1/dev/jewson_bbc/web/app/themes/bbc/vendor/soberwp/controller/src/Loader.php(119): ReflectionClass->__construct('App\\Controllers...')
#1 /home/chapterserver1/dev/jewson_bbc/web/app/themes/bbc/vendor/soberwp/controller/src/Loader.php(49): Sober\Controller\Loader->setClassesAlias()
#2 /home/chapterserver1/dev/jewson_bbc/web/app/themes/bbc/vendor/soberwp/controller/controller.php(22): Sober\Controller\Loader->__construct(Object(Brain\Hierarchy\Hierarchy))
#3 /home/chapterserver1/dev/jewson_bbc/web/wp/wp-includes/class-wp-hook.php(286): Sober\Controller\loader('')
#4 /home/chapterserver1/dev/jewson_bbc/web/wp/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#5 /home/chapterserver1/dev/jewson_bbc/web/wp/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#6 /home/chapterserver in /home/chapterserver1/dev/jewson_bbc/web/app/themes/bbc/vendor/soberwp/controller/src/Loader.php on line 119

On moving my site to it’s staging server and I’ve no idea why. It’s working fine on my local machine which is a Mac running High Sierra. I’ve run composer update, synced the database. But I’m continuously getting this problem with FrontPage controller. My namespace is App\Controllers as it should be and file is named FrontPage.php as per PSR4 requirements.

Any ideas?

Thanks
Kevin

How did you move the site?

First I used GIT, just pulled my files, ran composer update etc, uploaded the dist folder and synced the database. That didn’t work so I just zipped up the whole lot and uploaded that, and that didn’t work either - getting the same error both ways. So I’m confused!

It had been working fine on staging, up until I ran composer update but I had to do that because I needed v2

It’s a little unclear to me what context some of these commands are being run in, i.e. did you run composer update on your local server or your staging server? I’m also not sure what you you mean by “because I needed v2”. Are you referring to version 2.0.0 of Controller? Where did you run composer update in this context?

Without more information about your local development environment, your staging environment, and how/when/what you’ve been moving between the two, it’s difficult to debug what the problem might be, so here’s some generalize information on how these things are loaded that may help you get to the bottom of the problem:

PHP Classes in Sage are accessed through autoloading. Autoloading works by giving PHP instructions for how to find things. The simplest version is something like “When a script asks for the class SomeClass, we can find the files for that in /src/.” The exact instructions for how to find and load something are not built in to PHP—they have to be created in a script. In the case of Sage, it leverages Composer to do this for it. If you look in the composer.json file in the root of your theme, you’ll see something like this:

"autoload": {
    "psr-4": {
      "App\\": "app/"
    }
},

This tells Composer “I have some files in app/ that are in the App namespace, so set up an autoloader that loads App classes from that directory using the PSR-4 standard.” When you run composer update or composer dumpautoload (or several other commands), composer looks at that rule (and many other internal rules and ones passed to it by packages) and builds at that time an autoloader for all of the classes and files it has been made aware of.

That Composer-generated autoloader is how Controller is able to find, and load, your controllers, because they are in the App namespace. The autoloader is created (by default) in the vendor directory in your theme, along with all the other dependencies that Composer installs. It’s important that the autoloader is updated when your theme organization changes, and that you upload the vendor folder (and consequently the autoloader) when you move updates to your staging site.

If you are moving your site from/to systems that have different case-sensitivity, it’s possible for that to cause issues, because the autoloader relies on directory names inside your theme.

3 Likes

@alwaysblank, thank you for the reply! I do appreciate your help as ever. Sorry if I was a little unclear. To add a little context then, I’ve been developing my site locally for a few days on my Mac - standard config, Apache, PHP 7, OS 10.13.2. You may remember that a few days ago in order to access some ACF fields I updated to version 2 of the controller on my dev site and you helped me with changing a couple of namespaces and filenames at that time. I’d already set up a staging site for this project and it was working fine but I hadn’t deployed anything since running composer update on the local dev. My usual deployment process is simply to push my theme files to Bitbucket, then on the staging server pull the changes. I then upload the dist file via FTP and finally I have a plugin which automatically uploads the database and the media library files - I’ve done this numerous times already and am very happy with it as a general rule. I did all that and realised when I viewed the staging site that none of the ACF stuff was displaying, the front page was displaying but I just had holes where my ACF field data should have been. Thats when I remembered that I’m an idiot and hadn’t run composer update on the staging site, therefore my front page would not have access to the ACF data. So that’s what I did, ssh’d in to the staging server and ran composer update in the theme folder. It updated fine, no errors. But now when I view the staging site I’m getting the above error.

Update… so I’ve now set up a new staging site with a fresh install of Bedrock/Sage, installed the plugins I need, ran composer update in the theme to get the newest version of Controller. I then uploaded my theme dist app and resources and synced the database. Everything works now apart from the ACF data! I’ve just got holes where that is supposed to be, but I’m not getting any of the errors I had before either. for the Home page, I have a controller called PageHome.php in app/controllers that looks like this:

<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class PageHome extends Controller
{
    protected $acf = true;
}

But I can’t access any of the ACF data on the homepage. And just to be clear, this is only on my staging server, on my local dev machine, this all works fine

Kevin

It’s probably an issue with case sensitivity.

It seems to be fixed in Sober Controller 2.0.1, but check the Sober docs and make sure you are using the correct folder capitalization in app/.

Also check your repo. There’s issues where pushing case changes from a Mac does not change case in the repo, and/or results in two folders/files being delivered to the server.

1 Like