Roots Discourse

[Sage 10] Composers/Controllers not working when adding a custom function

I’m using Composers, specifically the default ones such as App.php which has siteName function.

I’ve tested that function, and it works, however, there’s an issue which I can’t seem to figure out, because when I add a new function to it, it doesn’t work.

This is my app file:

<?php

namespace App\View\Composers;

use Roots\Acorn\View\Composer;

class App extends Composer

{

    /**

     * List of views served by this composer.

     *

     * @var array

     */

    protected static $views = [

        '*',

    ];

    /**

     * Data to be passed to view before rendering.

     *

     * @return array

     */

    public function with()

    {

        return [

            'siteName' => $this->siteName(),

        ];

    }

    /**

     * Returns the site name.

     *

     * @return string

     */

    public function siteName()

    {

        return get_bloginfo('name', 'display');

    }

    

    public function AAA()

    {

        return "test";

    }

}

Site name works, I even copied and just modified function name, and nothing.

I called it in the page and it rendered the site name correctly. However, when using my function, in this case $AAA - it broke the site with a death screen.

I have no idea why.

Also, another issue, I’ve created Page.php composer, try to use it on page.blade.php and it doesn’t work.

I have no idea what’s wrong with this.

The only variables passed to your view are the ones in the array returned by with().

So why is this working:

public function siteName()

    {

        return get_bloginfo('name', 'display');

    }

But when I copy and past that, and change the function name, it doesnt?

Because your with() method is already returning the value of that method:

/**

     * Data to be passed to view before rendering.

     *

     * @return array

     */

    public function with()

    {

        return [

            'siteName' => $this->siteName(),

        ];

    }
1 Like