Append query to `$page->go()`

Is there a way to append a query, e. g. ?foo=bar, when using $page->go()?

Sure with $page->go($options)

$page->go([
  'query' => [
    'foo' => 'bar'
  ]
]);

Is it possible that it’s not working when I calling go() with options for the page I already on? Like:

$current->go([
    'query' => ['foo' => 'bar']
]);

I see my browser reloading the page but no query appended. If I try to go on another page, it works.

This is expected behavior on current page. You should check that query is empty. Not tested but something like that:

if (empty(Url::query()) === true) {
  $current->go([
    'query' => ['foo' => 'bar']
  ]);
}

Well, I see that the query is empty: it‘s not in the URL. But why isn‘t it appended on go? That doesn‘t make sense.

Where are you using this method?

In a controller after processing POST data. I need to redirect to clear the request data and would like to append a query string to flag the success.

And the page is actually reloading. I just can‘t get it to append the query. If I redirect to a different page than the current one, then it works.

Hm, I set up the following very basic use case:

Template form:

<form action="" method="POST">
  <input type="submit" name="submit" id="submit">
</form>

Controller:

return function ($kirby, $page) {

    if ( $kirby->request()->is('POST') ) {
        $page->go([
            'query' => ['foo' => 'bar']
          ]);
    }

    return [];

};

which does what I expected, i.e. redirect to the page appending the query on submit :thinking:

What happens if you add this before the go?

$page = $page->update();

It‘s still the same page but not the same page object.

Oh, I think I‘ve got an idea why it‘s not working on my end: there is a route definition in a plugin that might remove the query! I‘ll have to check that but that sounds like a logical mistake at my end.

That doesn’t make a difference, it still redirects.

I found the issue! It was a custom url method in a page model that didn’t take the passed options into account. I wasn’t aware that the url method of a page also had to handle the dynamic query part.

Case solved. Thanks for your assistence!