How attach view composer/controller to a view inside a plugin (Clover based)

So I’m building a plugin (using Clover as a base) and trying to use Sage 10 as a reference for how to structure

Inside PluginNameServiceProvider->boot() I call the following to setup a namespace for the blade views inside the plugins context…

public function boot()
      if ( function_exists( '\Roots\view' ) ) {
        $view = \Roots\view();

        $view->addNamespace('MyNamespace', __DIR__ . '/../../resources/views/');

As a result, I can use Roots\view inside a provider to return & parse a blade file in the plugin directory …

return \Roots\view('MyNamespace::app')->render();

app.blade.php is stored as you’d expect in plugin/resources/views/app.blade.php

I’ve then created a controller/View composer in plugin/src/View/Composers/App.php
Contents as:


namespace PluginNamespace\View\Composers;

use Roots\Acorn\View\Composer;

class App extends Composer
    protected static $views = [

    public function with()
        return [
        'withtest' => 'test passed',

but the controller isnt getting picked up and passing data to the view from the array returned from with

Any idea how im should attach the controller? 

I suspect I'm going about this backwards and I should somehow be instantiating a view composer and then using this to render an associated view ?!

Suspect maybe i have to do something like:

$appctrl = new \PluginNamespace\View\Composers\App();


Huzaa !
go laravel! Ive no idea what im doing so i referenced Views - Laravel 10.x - The PHP Framework For Web Artisans
In the service providers file, i just added

if (function_exists('\Roots\view')) {
            $view = \Roots\view();

            $view->addNamespace('PluginNamespace', __DIR__ . '/../../resources/views/');

        Facades\View::composer('PluginNamespace::app', App::class);

And now when i render the view, the appropriate class is used !

Whilst this “works” - I’m still interested to find out if theres a more correct way of doing things inside Clover / Acorn as I cant find any official documentation to guide me.

What you’re doing is fine. You might be able to make use of $this->app->view or something.

Otherwise, you can check out the acorn-example-package, but it’s not using Composers.

Thanks for the guidance Log1x - yeh I was just peeking into the ViewServiceProvider, attachComposers and thinking I might be able to do $this->app->view or something but also wasnt sure if I had to extend a certain class to get that to work.

I’m using a really old plugin (wp-router) to generate routes at the moment - do you know if there’s anything in Acorn I can be making use of to do the same and make custom routes or can recommend a preferred package?

Essentially made a RouteServiceProvider that does this:

$route_args = array(
        'path' => '^threetest',
        'query_vars' => array(),
        'page_callback' => function () {
            return \Roots\view('ThreeSketch::app')->render();
        'page_arguments' => array(),
        'access_callback' => true,
        'title' => 'threetest',

        $router->add_route('threetest', $route_args);

Huge shouts for your work btw on acf composer/all the sage bits. They’ve accelerated my WP development 100 fold!

the acorn example package is a great start , didnt know that existed. Thanks