Jump to content

Managing URL query strings when outputting links


Vigilante
 Share

Recommended Posts

I found one or two threads with similar issue such as this one: 

 

But that is 2012 and I wonder if a newer technique is built into the core yet.

For some reason this feels really complex when it shouldn't be.

Simple example, outputting a grid of stuff. It's paginated and I use query string for sorting and other values (i.e. "cat=100&sort=az").

The problem is when I need to output a standard link with another query string. I want to included existing query strings. Well this is easy enough as I can grab $input->whitelist->getArray() and then run http_build_query. But the problem is what if the query string is already there? I don't want to output it twice. So I have to take my array, check if the query string key exists, then change it, before running http_build_query. Then do this for every link I output everywhere on the page.

I check if that query string is already in the URL, and add it if not, and then assemble a new link. It also becomes more difficult with logic when deciding if I need to add the question mark, or the ampersand. 

I could probably write a custom function and include it globally in the theme, but this should be easier! Something like a link builder. For example $input->makeLink($page->id, true, ['cat'=>100]);
or whatever. But say, I need to make a link, set it to the current page, included existing query string, but set cat to 100 whether it already exists or not.

If the current page is "/page/page2?cat=100&sort=az" and they click a link to sort z-a, it just needs to keep the same URL, realize sort is already there, change it to za, and return my string URL to put in a link. Perhaps `<a href="<?=$input->makeLink(['sort'=>'za')?>">Sort ZA</a>`. And it would output current page with existing query string, changing or adding sort to za.

Of course, such a function could make use of $input->whitelist too, whatever it may be.

If a function like already exists, I haven't found it. But I must be missing something or an doing this the hard way. I've got foreach and if blocks all over my template just trying to place links here and there without messing up the query string.

Help!

Link to comment
Share on other sites

Hi @Vigilante

Take a look at this page to find out all available options how you can build your URL

https://processwire.com/api/ref/page/url/

You can get current URL by $input->url and get current URL with query string by @input->url(true). Take a look at this 

https://processwire.com/api/ref/input/url/  and  https://processwire.com/api/ref/input/query-string/

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...

Ok that really helped. I can use the `$input->queryString(array())` method to good effect.

The only problem is that I have to create links in an ugly way, all over my template, stuff like this:

`<a href="<?php $page->url() . '?' . $input->queryString(['key'=>'val'])?>">Link</a>`

I guess I could build a custom function but it would be nice to have all that rolled into one, something like `<?php$page->url(true, ['key'=>'val']);?>`. Where 'true' is something like "include segments and query string" and the array would add or replace a particular attribute.

I actually did try to do a function but ran into trouble with scope issues or something, not sure, but the page just wouldn't load. So instead I have that long ugly url generation string above everywhere I have to output links on pages where query strings are important. I don't want to lose existing parameters when any links are clicked, and I want to easily update or add a new parameter as needed. And it all still needs to work with the automatically pagination function in PW. The pagination links can't erase any URL parameters either.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...