contributte / menu-control

🍔 Menu and breadcrumb components for Nette framework (@nette)
MIT License
28 stars 20 forks source link

Invalid link when adding action which needs parameters #27

Closed foxycode closed 4 years ago

foxycode commented 6 years ago
Invalid link: Missing parameter $id required by App\Presenters\VpsDetailPresenter::actionDefault()

See that I turned of menu visibility and I am also not using breadcrumbs in application. Is it bug or is there any way how to handle this? There was include statement in earlier version which doesn't work anymore. That's why I added detail as separate item.

Menu definition:

menu:
    default:
        templates:
            menu: %appDir%/Components/Menu/menu.latte
        items:
            VPS:
                link: '#'
                items:
                    VPS:
                        action: VpsList:default
                    VPS detail:
                        action: VpsDetail:default
                        visibility:
                            menu: FALSE

Presenter:

final class VpsDetailPresenter extends BasePresenter
{
    public function actionDefault(int $id): void
    {
    }
}

Menu template:

<ul class="nav navbar-nav" n:if="$menu->hasVisibleItemsOnMenu()">
    {foreach $menu->getVisibleItemsOnMenu() as $item}
        {if $item->hasVisibleItemsOnMenu()}
            <li n:class="dropdown, $item->isActive() ? active">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">{$item->getRealTitle()} <span class="caret"></span></a>
                <ul class="dropdown-menu">
                    <li n:foreach="$item->getVisibleItemsOnMenu() as $subitem" n:class="$subitem->isActive() ? active">
                        <a href="{$subitem->getRealLink()}">{$subitem->getRealTitle()}</a>
                    </li>
                </ul>
            </li>
        {else}
            <li n:class="$item->isActive() ? active">
                <a href="{$item->getRealLink()}">{$item->getRealTitle()}</a>
            </li>
        {/if}
    {/foreach}
</ul>
davidkudera commented 6 years ago

Hi, that seems like two problems. Missing links' parameters in neon config and include. Am I correct?

The first one should be fixed, I totally forgot about parameters.... 👼

And about the second "problem".. Right now I'm using something like this in my presenters:


// BasePresenter:

abstract class BasePresenter extends Presenter
{

    private $menuContainer;

    public function injectMenu(MenuContainer $menuContainer)
    {
        $this->menuContainer = $menuContainer;
    }

    protected function getMenu(): IMenu
    {
        return $this->menuContainer->getMenu('default');
    }

}

// User DetailPresenter:

final class DetailPresenter extends BasePresenter
{

    private $usr;

    public function actionDefault(int $id): void
    {
        $this->usr = loadUserSomehowById($id);

        $this->getMenu()->getItem('users')->addItem('detail', $this->usr->getName(), function(IMenuItem $item) {
            $item->setAction('this');
        });
    }

}

The problem with previous solution was that with large apps it was difficult to keep track of when the PHP method was used and when the neon one. Plus to be honest I wasn't totally sure if it worked correctly 😃

In hopefully near future I plan to add some kind of "items fragments". These should be small functions which contains the code from action* methods for building dynamic menu items.

So instead of writing this large code above in almost all presenters, you could just use fragment, which wraps that code inside and which you can simply reuse everywhere you want.

foxycode commented 6 years ago

Ok, there are definitelly action prameters missing. I tried you example and it's working, but it's like scratching your left ear with right hand. It would be nice if I could get MenuContainer form MenuComponent for start.

Another thing is, there is bug i think. When I add that detail into menu config, it shouldn't create link at all, if I don't want it to be present in menu and when I'm not in detail: https://i.imgur.com/UbqVeF1.png

I'm not sure what you mean by item fragments.

foxycode commented 4 years ago

Obsolete, closing.