Comments (6)
Excellent points, I'm definitely up for any suggestions to improve the documentation.
Seems I forgot to update the quick start page in regards to the service provider suggestion. I'll definitely be adding a dedicated page to go over the possible locations one can register menus/menu items in - will be easier to maintain none the less!
I'll keep this issue open in the mean time until a page is added~
from menus.
@fredrkr you can also define a route middleware and use it for certain routes. This way you can decide which pages you want to build the menu.
from menus.
@hcancelik Sure, that's an option, too :-) What I said about the middleware-way still applies, though. That is, if the "menu-middleware" is defined as global, the code will be executed whether you output the menu, or not.
My personal favorite of the bunch is using a view-composer, though. It just resonates better within the vast empty space that is my head. Menus are inexorably related to views, after all. That said, there is nothing wrong with using the other approaches either :-)
from menus.
A problem with View Composers: they are called multiple times during the life-cycle of Laravel. I'm thinking now on a way to avoid this.
from menus.
That has never been a problem for me. I always use one top-level layout-view that all other non-partial views extend. It's this view that I hook up to the view composer that houses the Menu::make()-call. I haven't seen that code executed more than once for each request.
Under which circumstances do you think the view composer-approach would be problematic?
from menus.
I had a problem with this yesterday and i'll try to explain:
Obs: Sorry for the giant text, at the end, plz tell me if you understood the approach
I'm making a submenu to be rendered on multiple views, automatically. To "construct" the structure of this menu, i created the middleware "MySubMenuMiddleware". This middleware has the following responsibilities:
-
Init this Menu package with:
$this->submenu = \Caffeinated\Menus\Menu::make('submenu', function($menu){ })
-
Identify all items from this specific menu from a source. (i can use Database, external files with JSON's, Arrays, etc, it doesn't matter, on condition that i have those itens with a "Title" and an optional "URL")
-
Add each item on the initial Menu:
$this->submenu->add(SOURCE_TITLE, SOURCE_URL)
The menu built by this middleware is the default submenu to be rendered on all my views, so, my top level layout holds a partial.submenu.blade.php file. It something almost like the example from the wiki https://github.com/caffeinated/menus/wiki/Blade-Bootstrap-Template
Nice, it's working. But now comes the problem:
Imagine that my default submenu has only two items: Do Action 1 and Do Action 2. Besides those default submenu items, i need to ADD a new item, BUT ONLY for a specific page. (Let's call it custompage.blade.php).
To be able to do that, i need get the menu and modify him before the partial.submenu.blade.php be rendered.
Here i have two options (please notify me if we have others possibilities):
- Use the CustomPageController to add a new item to this submenu.
- Use the CustomPageViewComposer to add a new item to this submenu.
If i use the Controller everything works fine, only one CUSTOM_TITLE item is add:
class CustomPageController extends Controller
{
public function index()
{
$submenu = \Caffeinated\Menus\Menu::get('submenu'); // remember, that's the name we use above to Make this menu
$submenu->add('CUSTOM_TITLE', 'CUSTOM URL');
return view('custompage');
}
}
But if i use the ViewComposer, whenever the CustomPageViewComposer is fired, my code will add this custom item. ( and this will depend on how many partials you have, cause it will be fired whenever a view is called).
class AppServiceProvider extends ServiceProvider
{
public function boot(ViewFactory $view)
{
$view->composer('otherpages::*', 'App\Http\ViewComposers\CustomItemsComposer');
}
}
class CustomItemsComposer
{
public function compose(View $view)
{
// the same code as the controller
$submenu = \Caffeinated\Menus\Menu::get('submenu');
$submenu->add('CUSTOM_TITLE', 'CUSTOM URL');
}
}
from menus.
Related Issues (20)
- How to get Icons only HOT 2
- Laravel 5.6 HOT 1
- Call to undefined method asUl HOT 1
- Removing methods & breaking BC HOT 2
- Call to undefined method asUl
- PHP 7.4: Trying to access array offset on value of type null at caffeinated/menus/src/Menu.php:66 HOT 2
- Call to undefined method Caffeinated\Menus\Item::attributes() (0)
- ErrorException in Menu.php line 90: Trying to access array offset on value of type null
- Laravel 8 support
- Laravel 5.2 HOT 3
- ->attributes() is rendering double quotes HOT 1
- Menu Cache? HOT 1
- I use localization and need some help HOT 1
- Icon for children
- Module Menus? HOT 1
- Active Link Detection on SSL/HTTPS
- Children attributes HOT 1
- https url HOT 1
- Laravel 5.4 HOT 5
- Instalation laravel 5.5.3 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from menus.