Code Monkey home page Code Monkey logo

Comments (6)

kaidesu avatar kaidesu commented on August 22, 2024

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.

hcancelik avatar hcancelik commented on August 22, 2024

@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.

piperone avatar piperone commented on August 22, 2024

@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.

 avatar commented on August 22, 2024

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.

piperone avatar piperone commented on August 22, 2024

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.

 avatar commented on August 22, 2024

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:

  1. Init this Menu package with:

    $this->submenu = \Caffeinated\Menus\Menu::make('submenu', function($menu){ })

  2. 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")

  3. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.