How to create a controller file for a custom template page in Sage 9

does anyone know how to create a controller file for a custom template page?? I have a template page named template-about.blade.php. how do I create its controller page?


Controller docs explain this:
The mapping from WordPress template names to controller class names is essentially: Convert the WordPress body class that corresponds to the template you want to CamelCase. i.e. page-template-about => PageTemplateAbout, front-page => FrontPage, etc.


@alwaysblank Thank you

Sorry for necroposting but I can’t figure this same problem out. I checked the linked controller docs and I think I followed everything, still can’t make it work.

I have a custom template with the filename of template-custom.blade.php. This is also set as my front page currently.

I set up a controller file called template-custom.php. Here is the content:


namespace App;

use Sober\Controller\Controller;

class TemplateCustom extends Controller
  public function AnotherTest()
		return 'eh';

I used camelcase as you can see, but still get the Undefined variable: another_test error. when I call it in the view file like this:


Now if I put this AnotherTest function in one of the premade controllers, like app.php or front-page.php it works. If I make some another simple controller file, like page or home, it doesn’t, even though I have those classes in the body for this page. Also, there is no front-page class but that controller still works so I’m not sure if I entirely get this body class based checking.

What did I miss here, do I need to include these new controllers in one of the setup/config files?

Thanks in advance. Greatly appreciated.

Check that you’re using the latest version of soberwp/controller (currently 2.0.1).

If you have that, then your problem would be because your controller namespace is incorrect.

Instead of namespace App; it should be namespace App\Controllers;

Refer to the premade app.php controller for example.

By default, endpoints can only receive data from two controllers: App, which passes data to all endpoints, and WhateverWordPressTemplateMatches, which is the name for that endpoint in the WordPress template hierarchy. What this means is that if an endpoint could potentially match two different WordPress “templates” that both have Controllers, one will override the other, which is what I suspect is happening here.

You’ve set a page template of template-custom on a page, and then made that page your front page. This means that your page is matching two “templates” with existing controllers:

  • front-page matches controllers/front-page.php
  • template-custom matches controllers/template-custom.php

My guess is that Controller matches your endpoint to front-page, overwriting any and all data from template-custom. You can test this by renaming controllers/front-page.php, which (if I’m right) will cause your $another_test variable to be available in your Blade.

You can get around this in one of two ways:

  • Move your logic from the template-custom controller to the front-page controller (this is what I would do).
  • Tell template-custom to inherit the tree, using the instructions in the Controller documentation.

Thanks, alwaysblank, that sounds pretty plausible. Will check this in a minute. I wanted to make it based on the custom template because it’s not going to be the home page always but in this case it is, I guess your second solution would work in this case?

Thanks you so much alwaysblank. I think that it will help a lot of people if you mention correct template file naming in controller documentaion

I’m glad I was able to help, but I’m not involved with soberwp/controller. If you wanted to submit a pull request to clarify the docs, I sure the maintainer would be happy to consider it, though.