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

Invalid link with persistent parameter #43

Open
olexap opened this issue Feb 14, 2020 · 9 comments
Open

Invalid link with persistent parameter #43

olexap opened this issue Feb 14, 2020 · 9 comments

Comments

@olexap
Copy link

olexap commented Feb 14, 2020

Found a bug.

Trying to use route with language parameter which is persistent parameter in presenter.
All links generated in project are okay but links in menu-control don't respect actual value of the parameter. Instead there are just default parameter value for route.

@foxycode
Copy link
Member

Thanks for reporting. Can you please add example code how to induce this bug?

@olexap
Copy link
Author

olexap commented Feb 14, 2020

web-project.tar.gz

Adding clean nette web project with implemented navigation with showing generated link and plink in template (they are ok) and menu (not ok)

Try to change language string in url and you see that links are changes but language in menu keep using default from router, not actual value of the variable.

Tested on PHP7.4

@olexap
Copy link
Author

olexap commented Feb 16, 2020

I trying to write my own menu component and I end at the same problem when I trying to generate url myself (not using latte). It seems like problem is LinkGenerator in nette. It ignores parameters defined in route and use defaults instead.
I have no idea how to make it work.

@foxycode
Copy link
Member

Yes, this will be probably the cause, because menu-control uses LinkGenerator which don't known about current state of presenter persistent parameters and thus uses default values.

Solution would be create your own implementation of ILinkGenerator and use Presenter for link generation, but we get rid of Presenter earlier, because we had some problems I don't remember.

@olexap
Copy link
Author

olexap commented Feb 16, 2020

Yes. Nette LinkGenerator is wrong. I wandering that maybe will be possible to use RouteList and Request to make own LinkGenerator but I don't know how to access parameters from router :(
Dead end at the moment.

@patrickkusebauch
Copy link

For somebody, who stumble upon this as I did, here is a hacky workaround:

use Carrooi\Menu\IMenuItem;
use Carrooi\Menu\LinkGenerator\ILinkGenerator;
use Nette\Application\Application;
use Nette\Application\UI\Presenter;

final class NetteLinkGenerator implements ILinkGenerator
{

    private Presenter $presenter;

    public function __construct(Application $application)
	{
		$this->presenter = $application->getPresenter();
	}


	public function link(IMenuItem $item): string
	{
        if(($action = $item->getAction()) !== null) {
            return $this->presenter->link($action, $item->getActionParameters());
        }

        if (($link = $item->getLink()) !== null) {
            return $link;
        }

        return '#';
	}

}

The caveat is that now it recognizes relative vs absolute links, so in your config file, all your links now have to start with a colon.

I can give you no warranty, I just tried this out and persistent parameters works, but I have no idea if something else isn't broken.

@foxycode
Copy link
Member

Since this is not menu-cottrol bug, closing issue.

@patrickkusebauch
Copy link

patrickkusebauch commented Mar 30, 2020

@foxycode I guess this really depends on the point of view. Since it is a presented as Nette component, but it does not work fully with Nette. It should be at least mentioned somewhere in the documentation, WDYT?

@foxycode
Copy link
Member

@patrickkusebauch You're right. Will think about it.

@foxycode foxycode reopened this Mar 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants