-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Comments
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() }} |
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! |
Use this: public function register()
{
\Menu::macro('main', function () {
return Menu::new()
->add(Link::to('/', 'Home'))
->add(Link::to('/about', 'About'));
});
} |
Still doesn't work Method main does not exist. (View: E:\webdev_localhost_laravel\qw\resources\views\includes\sidebar.blade.php) |
Have you registered your service provider in config/app.php?
On Tue, 20 Jun 2017 at 00:05, Ovidiu Ungureanu ***@***.***> wrote:
Still doesn't work
Method main does not exist. (View:
E:\webdev_localhost_laravel\qw\resources\views\includes\sidebar.blade.php)
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#52 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAdiDQJKwgqnKTIfes3hJJKyNQ4a7_uEks5sFvCdgaJpZM4N-b_S>
.
--
Freek Van der Herten https://spatie.be +32 495 84 27 91
|
Hey, it worked. 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. |
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
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.
The text was updated successfully, but these errors were encountered: