Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a generated menu in all views #52

Closed
uovidiu opened this issue Jun 19, 2017 · 6 comments
Closed

How to use a generated menu in all views #52

uovidiu opened this issue Jun 19, 2017 · 6 comments

Comments

@uovidiu
Copy link

uovidiu commented Jun 19, 2017

Hi there,
new to Laravel (5.4)

I've created a basic menu.
To test, I placed the code in my HomeController but obviously rendering only works in the view called in the controller.

My blade structure is as follow
home.blade -> @extends('layouts.blank')
blank.blade -> @include('includes/sidebar')
sidebar.blade -> contains {!! Menu::main() !!}

So when I'm using home I can see the menu but any other view that extends blank doesn't render the menu.

Where can I use a basic menu defined like that

$menu = Menu::new([
    Link::to('/', 'Home'),
    Link::to('/about', 'About'),
]);

and have it available in all views?
Probably a service controller or a middleware but to be honest since I'm new to Laravel I don't have a clue.

Thanks.

@freekmurze
Copy link
Member

freekmurze commented Jun 19, 2017

Hi,

a service provider would be nice place to but your menu definitions.

The Laravel documentation contains a dedicated page on how to write and register service providers: https://laravel.com/docs/5.4/providers#writing-service-providers.

So in that service provider you could set up that menu like this:

Menu::macro('main', function () {
   return Menu::new()
      ->add(Link::to('/', 'Home'))
      ->add(Link::to('/about', 'About'));
});

And then you can use it in any blade view:

{{ Menu::main() }}

@uovidiu
Copy link
Author

uovidiu commented Jun 19, 2017

I have this in my service provider (MenuServiceProvider.php)

    public function register()
    {
        //
	    $this->app->bind('Spatie\Menu\Laravel\Menu', function () {
		    Menu::macro('main', function () {
			    return Menu::new()
				    ->add(Link::to('/', 'Home'))
				    ->add(Link::to('/about', 'About'));
		    });
	    });

    }

Also added Quickwater\Providers\MenuServiceProvider::class, in app.php.

But when I try to call it I get this error

Target [Spatie\Menu\Laravel\Menu] is not instantiable. (View: E:\webdev_localhost_laravel\qw\resources\views\includes\sidebar.blade.php)

To be honest the whole service controllers is a bit too much for me since I first met Laravel. I'm watching now Laravel 5.4 From Scratch: The Service Container

If you can help me some more with a full example it would be great.

Thanks for your answer!

@freekmurze
Copy link
Member

freekmurze commented Jun 19, 2017

Use this:

public function register()
{
    \Menu::macro('main', function () {
        return Menu::new()
            ->add(Link::to('/', 'Home'))
            ->add(Link::to('/about', 'About'));
    });
}

@uovidiu
Copy link
Author

uovidiu commented Jun 19, 2017

Still doesn't work

Method main does not exist. (View: E:\webdev_localhost_laravel\qw\resources\views\includes\sidebar.blade.php)

@freekmurze
Copy link
Member

freekmurze commented Jun 19, 2017 via email

@uovidiu
Copy link
Author

uovidiu commented Jun 20, 2017

Hey, it worked.
Thanks a lot!

I think you should put the usage in the README, I bet I won't be the only one not knowing how to properly use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants