Controller is not working

I can’t make the controller communicate with blade files. I’m out of ideas. If someone could give me a help that would be much appreciated. Below the settings I’m currently using this project and the files I changed:

“roots/sage-lib”: “~9.0.1”,
“soberwp/controller”: “~9.0.0-beta.4”

site/app/themes/my-theme/app/Controllers
App.php
AppHome.php
FrontPage.php
TemplateIndex.php

App.php

<?php
namespace App;

use Sober\Controller\Controller;
use Roots\Soil\Utils;

class App extends Controller {
    public function listAwards()
    {
        $awards = new \WP_Query(array(
            'post_type' => 'awards',
            'posts_per_page' => '1',
        ));

        $data = [];
        while ($awards->have_posts()) {
            $awards->the_post();

            if (have_rows('awards_custom_field')) {
                while (have_rows('awards_custom_field')) {
                    the_row();
                    $awards_list['title'] = get_sub_field('award_title');
                    $awards_list['years'] = get_sub_field('award_years');
                    array_push($awards_list, $data);
                    $awards_list = [];
                }
            }
        }
        return $data;
    }
}

site/app/themes/my-theme/resources/views/layouts
app.blade.php
<!doctype html>
<html @php(language_attributes())>
@include(‘partials.head’)
<body @php(body_class())>
(…)
{!! $listAwards !!}

Controller converts variables to snake_case when exporting them to the Blade. Try

<!doctype html>
<html @php(language_attributes())>
@include(‘partials.head’)
<body @php(body_class())>
(…)
{!! $list_awards !!}

This is detailed in the documentation:

  • The method name becomes the variable name in Blade
  • Camel case is converted to snake case. public function ExampleForUser in the Controller becomes $example_for_user in the Blade template
2 Likes

You’re right. Thanks!

I would recommend upgrading to 2.x.x as well.

1 Like