3

I am currently setting up routes for a one-page application. As soon as a Query Parameter is introduced, the route fails. The route "test" replaces "my-av1" in the following photos.

The documentation in Craft does not describe how Query Parameters are handled. How do I proceed from here?

Routes should work as described – but as soon a Query Parameter is introduced in the url, the route stops working. The route "test" replaces my-av1

2

Routes can't include query strings. A route is a path to a template file (or a controller action, if you've written a custom plugin).

You can retrieve a query string value from within a template using the craft.request.getParam method.

For example:

{% set token = craft.request.getParam('token') %}

Update

Following the conversation in the comments, here is an example of this working with a standard Craft route.

My example route /querystring/test points to the template _views/querystring-test.twig, which contains the following example code.

<h1>Query String Test</h1>

{% if craft.request.getParam('example') %}
    <p>The query string value for <code>example</code> is "{{ craft.request.getParam('example') }}",</p>
{% else %}
    <p>There is no query string parameter.</p>
{% endif %}

Here is a screenshot of the page without a query string:

enter image description here

And here is a screenshot of the page with a query string:

enter image description here

7
  • I'm just trying to make sure that the template file loads even though the URL has a query parameter. I'm thinking that should be possible and that it is a rather common use case. Am I wrong? May 24 '17 at 18:32
  • The route will match, and the template will load, regardless of whether the URL contains query parameters. May 24 '17 at 19:53
  • I thought that would be the case, but if you have a look at the screenshots above – that's not what's going on. Any ideas of what might be wrong here? May 24 '17 at 22:16
  • 1
    Have you tried appending the query string to /test/register, without the trailing slash? May 25 '17 at 0:20
  • 2
    Oh. After quite some time debugging we figured out that we're using a reserved Query Parameter. It works fine with anything besides the Query Param "token". (craftcms.com/docs/config-settings#tokenParam) We'll do the name change. ;) May 26 '17 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.