nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.52k stars 959 forks source link

Creating a view composer doesn't seem to work #1325

Closed mariusberget92 closed 2 years ago

mariusberget92 commented 2 years ago

I don't know if I need to do something different when using this package to get view composers to work, but I have done the same thing I always do with a non-modular Laravel app, but can't seem to get it working at all.

I created the ViewServiceProvider for the module.

<?php

namespace Modules\Dashboard\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Modules\Dashboard\Composers\SidebarComposer;

class ViewServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return [
            View::composer('dashboard::layout.master', SidebarComposer::class)
        ];
    }
}

Then I created my composer file:

<?php

namespace Modules\Dashboard\Composers;

use Illuminate\View\View;
use Nwidart\Modules\Facades\Module;

class SidebarComposer
{   
    /**
     * All modules.
     */ 
    protected $modules;

    /**
    * Create a new sidebar composer.
    *
    * @return void
    */
    public function __construct()
    {
        $this->modules = Module::all();
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('modules', $this->modules);
    }
}

Then lastly in the module service provider I registered the ViewServiceProvider like this:

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->register(RouteServiceProvider::class);
        $this->app->register(ViewServiceProvider::class);
    }

The problem is that when I try to call the $modules variable from the dashboard view file I only get that it is undefined. Is there something I am missing here?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.