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

Url view helper and controller plugin feature to reuse matched query args #22

Open
weierophinney opened this issue Dec 31, 2019 · 2 comments

Comments

@weierophinney
Copy link
Member

This issue has been moved from the zendframework repository as part of the bug migration program as outlined here - http://framework.zend.com/blog/2016-04-11-issue-closures.html


Original Issue: https://api.github.com/repos/zendframework/zendframework/issues/7350
User: @27cm
Created On: 2015-03-20T07:41:10Z
Updated At: 2015-11-06T20:58:33Z
Body
There is no easy way to reuse query arguments via url view helper or controller plugin. If current page has some query arguments (a view template doesn't know about them) and it's necessary to create url to this page, remove some query arguments or/and add some other arguments.

For example current page is /blog?sort=date&order=asc
It's necessary to create url /blog?sort=date&order=asc&page=2 - add argument page=2 and save other arguments, if they were.

$this->url(null, [], ['query' => ['page' => 2]], true); 
// returns '/blog?sort=date&order=asc&page=2'

$this->url(null, [], ['query' => ['page' => 2]], false); 
// returns '/blog?page=2'

Now current page is /blog?sort=date&order=asc&page=2
It's necessary to create url /blog?sort=date&order=asc - remove argument page and save other arguments, if they were.

$this->url(null, [], ['unset_query_params' => ['page']], true);
// returns '/blog?sort=date&order=asc'

To realize it I rewrote Url View Helper. But it isn't a good solution. I think it's necessary to go deeper, to Route.

So I would like to discuss this feature.



Originally posted by @GeeH at zendframework/zend-view#74

@weierophinney
Copy link
Member Author

I wrote my own url helper that wraps oryginal helper and adds functionality to reuse query parameters. I need to inject request to get current query.

public function __invoke($route = null, $params = [], $options = [])
{
    $requestQuery     = $this->request->getQuery()->toArray();
    $optionsQuery     = isset($options['query']) ? $options['query'] : [];
    $options['query'] = \array_merge($requestQuery, $optionsQuery);

    return $this->view->url($route, $params, $options);
}

Couldn't for some reason this be done in oryginal helper?


Originally posted by @bartoszzajac at zendframework/zend-view#74 (comment)

@cbichis
Copy link

cbichis commented Dec 26, 2022

Yes, on original helper. I don't see the need for separated helper for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants