Registering a Composer file from another directory, and have it load when a view parital is called

Im struggling with the concept of autoloaders, when trying to create dynamic components in my Sage10 setup.

What I am trying to achieve:

  • A directory that contains all the assets for one ‘component’ (blade, composer, scss, js…
  • Load/register its Composer class and tie it to the blade view, as normal
  • This directory could shrink or grow depending on what components are put in - references to these files have to be dynamic and not hardcoded

Why I am trying to achieve it

  • Create some more modular, reusable components in my Sage projects

What I am having trouble with:
I cannot seem to load/tie a composer file from another directory and have it work when the corresponding view is loaded.

is it possible to do something like the following (and if so can anyone spot what I am doing wrong?)

Templates.php gets autoloaded (in the Composers dir)

namespace App\Composers;

use Roots\Acorn\View\Composer;

require_once dirname(__FILE__) . "/../../../resources/views/components/test/Test.php";

And then that Test.php file:

namespace App\Composers;
use Roots\Acorn\View\Composer;
use App\Fir;

class Test extends Composer
{
    /**
     * List of views served by this composer.
     *
     * @var array
     */
    protected static $views = [
        'components.test.test'
    ];
    
    
    /**
     * Data to be passed to view before rendering.
     *
     * @return array
     */
    public function with()
    {
        return [
            "hello" => "world"
        ];
    }

}

Any help appreciated!

This topic was automatically closed after 42 days. New replies are no longer allowed.