Home controller not working correctly in home.blade.php view

I am using a home.blade.php file in a new Sage9 theme installed last night. I used the composer start-project method described in the Sage installation instructions. I’ve created a Controller file called home.php and it contains this:

<?php

namespace App;

use Sober\Controller\Controller;

class Home extends Controller
{
    public function what()
    {
        return 'weird';
    }

    public static function strange()
    {
        return 'strange';
    }
}

in home.blade.php:

$what: {{$what}}<br>
Home::strange(): {{Home::strange()}}  <br>

$what doesn’t return anything, but the strange() method is working correctly.

I’m wondering why the what() method is not loading into the template correctly. I was able to create a method that did work that way in the included front-page.php controller file. Is the Home controller named incorrectly? Should I just add my methods to the FrontPage controller?

Any help would be appreciated!
Thanks,
Aaron

Hey @cimocimocimo,

What page/path are you loading, and what do you have under “your homepage displays” in Settings -> Reading?

– Matt

That setting is “Your homepage displays -> Your latest posts”

I’m loading the homepage at the root of the site so http://testing.local/

The template file is in resources/views/home.blade.php alongside the other templates like index.blade.php and the rest.

Thanks for the info.

If you have both front-page.php and home.php in your controllers folder, looks like the front page controller takes precedence over the home controller, following the WordPress template hierarchy, even if you don’t have a front page template file.

Put this in your home template:

@debug('hierarchy')
$what: {{$what}}<br>
Home::strange(): {{Home::strange()}}  <br>

You should see this in the output:

Hierarchy Debugger:

  • controllers/app.php
  • controllers/index.php
  • controllers/home.php
  • controllers/front-page.php

If you remove your front page controller, your home controller will be used and $what will be populated.

– Matt

2 Likes

Aaaaaaaah, ok. That works now. I was using the @debug(‘hierarchy’) tag but since I saw the app.php file I thought the rest of the files were getting loaded as well. I guess that means that only one of index.php, home.php, and front-page.php gets loaded into the template then. With the order or priority from the bottom of that list.

Thanks so much for your help. Much appreciated!

1 Like