WooCommerce controllers for overridden template files setup

(Firstly, thanks to the Roots team and contributors for making WordPress development like a hundred times better!)

I’ve got sage-woocommerce installed. It allows me to happily override WooCommerce templates with blade.php views.

However, I’m not able to use controllers for these overridden views.

Let’s take the example of the WC template myaccount/form-login.php. I have a view at views/woocommerce/myaccount/form-login.php that works perfectly.

Then, I’ve tried creating a controller whose folder structure mirrors the overridden template: app/Controllers/woocommerce/myaccount/FormLogin.php with contents:

<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class FormLogin extends Controller
{
    public function helloWorld()
    {
        return 'hello world >:)';
    }
}

When I access the login page at http://localhost:3000/my-account/, I get errors:

Error and stack

Fatal error: Uncaught ReflectionException: Class App\Controllers\FormLogin does not exist in /app/web/app/themes/upl-ecommerce/vendor/soberwp/controller/src/Loader.php on line 121
ReflectionException: Class App\Controllers\FormLogin does not exist in /app/web/app/themes/upl-ecommerce/vendor/soberwp/controller/src/Loader.php on line 121 Call Stack: 0.0002 382568 1. {main}() /app/web/index.php:0 0.0003 382848 2. require(’/app/web/wp/wp-blog-header.php’) /app/web/index.php:6 0.0003 383232 3. require_once(’/app/web/wp/wp-load.php’) /app/web/wp/wp-blog-header.php:13 0.0003 383536 4. require_once(’/app/web/wp-config.php’) /app/web/wp/wp-load.php:42 0.0113 431864 5. require_once(’/app/web/wp/wp-settings.php’) /app/web/wp-config.php:9 0.1570 7074720 6. do_action() /app/web/wp/wp-settings.php:540 0.1570 7075096 7. WP_Hook->do_action() /app/web/wp/wp-includes/plugin.php:478 0.1570 7075096 8. WP_Hook->apply_filters() /app/web/wp/wp-includes/class-wp-hook.php:311 0.2565 8099448 9. Sober\Controller\loader() /app/web/wp/wp-includes/class-wp-hook.php:287 0.2566 8099672 10. Sober\Controller\Loader->__construct() /app/web/app/themes/upl-ecommerce/vendor/soberwp/controller/controller.php:41 0.2573 8110776 11. Sober\Controller\Loader->setClassesAlias() /app/web/app/themes/upl-ecommerce/vendor/soberwp/controller/src/Loader.php:49 0.2574 8112344 12. ReflectionClass->__construct() /app/web/app/themes/upl-ecommerce/vendor/soberwp/controller/src/Loader.php:121

Next, I tried putting the controller next to App.php at app/Controllers/FormLogin.php. This doesn’t error, but my view can’t access the data from the controller ($hello_world is not defined).

I have a feeling I’m missing something obvious. Any help would be much appreciated!

This topic was automatically closed after 42 days. New replies are no longer allowed.