get_the_ID() Not Working in Sage Controller

Hello, I hope you’re doing well.

I’m encountering an issue where get_the_ID() isn’t returning the expected ID within my Sage controller. I’m using the controller to handle some logic, but the retrieved ID seems to be incorrect.

My controller looks like this:

<?php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;

class Test extends Composer
{
    /**
     * List of views served by this composer.
     *
     * @var string[]
     */
    protected static $views = [
        '*',
    ];

    /**
     * Data to be passed to view before rendering.
     *
     * @return array
     */
    public function with()
    {
        return [
            'taxName' => $this->display_taxonomy_name(get_the_ID(), 'tax-name'),
        ];
    }

    /**
     * Display Taxonomy Name
     */
    public static function display_taxonomy_name($post_id, $taxonomy) {
        $terms = get_the_terms($post_id, $taxonomy);
        if ($terms && !is_wp_error($terms)) {
            $term_names = wp_list_pluck($terms, 'name');
            return implode(', ', $term_names);
        }
        return '';
    }
    
}

In my blade template (ACF Block), I’m attempting to showcase it with {!! $taxName !!}. However, it appears blank. Interestingly, when I directly integrate display_taxonomy_name(get_the_ID(), 'tax-name') into the template, it functions properly.

Thank you in advance for your assistance.

Generally, I think the way to handle that situation is to run the query in the Composer and build an array of values. Then you can iterate over that array in your view.

I often just do what you mentioned, where I put the function requiring the post id directly into the view.

Here is a similar post: Get data from view to composer from get_the_terms() - #2 by uraayush

Thank you @csorrentino
I’ll give it a try