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

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.
2 Likes