spatie / laravel-menu

Html menu generator for Laravel
https://freek.dev/414-a-modern-package-to-generate-html-menus
MIT License
914 stars 83 forks source link

Allow add menu by different packages #92

Closed thewebartisan7 closed 4 years ago

thewebartisan7 commented 4 years ago

I have a question how to solve something with this package, I hope someone can let me know.

I would like to know how to allow to add more links by different package on the same menu.

I don't want to use database to store the menu links, but PHP with macro.

I have a menu defined in a package, then I would like to allow other package to extend my menu by adding more links.

Thanks

thewebartisan7 commented 4 years ago

I think that I found in issue a solution, here https://github.com/spatie/laravel-menu/issues/53

There is other better way or this is still the only one?

Thanks!

thewebartisan7 commented 4 years ago

I was able to make this work, but without macro. I suppose there is no way with macro.

Seem the only way it's by using a singleton instance.

In my service provider I add:


    // Service Provider
    public function register()
    {
        // User Requests
        $this->app->singleton('mymenu', function() {
            return Menu::new()
                ->link('/', 'Home')
                ->link('/about', 'About');
                ->setActiveFromRequest();
        });
    }

// Somewhere else in application
app('mymenu')->link('/contact', 'Contact');

// Then in view the only thing that I can to render menu seem using alias, since there is not macro.
echo app('menu.sidebar');

Please let me know if there is better way to handle this

Thanks

riasvdv commented 4 years ago

That's a good way to handle it 👍