2

In a plugin, I am trying to redirect to a url, and set some values on the template. It's not working. Here is a simplified (not working) test case:

Template lives at templates/test.twig:

someVar is defined? {{ someVar is defined ? 'yes' : 'no' }}

<form method="post" action="" accept-charset="UTF-8">
  {{ getCsrfInput() }}
  <input type="hidden" name="action" value="site/test">
  <input type="hidden" name="redirect" value="/test">
  <button type="submit">Submit</button>
</form>

Controller code (which does get called):

public function actionTest() {
    craft()->urlManager->setRouteVariables(array(
        'someVar' => 23
    ));
    $this->redirectToPostedUrl();
}

What I expect, is that when I first visit the page, someVar isn't defined, but when I submit the form and revist the page, someVar will be defined (and equal 23). But it is not defined.

1

If you want to reload the form, you don't need to (indeed, shouldn't) redirect to the posted URL. That's why the form's action attribute is empty.

This should work:

public function actionTest() {
    craft()->urlManager->setRouteVariables(array(
        'someVar' => 23
    ));
}
| improve this answer | |
  • The problem with that I am filtering and paginating a bunch of entries. I might be on page 3 of pagination, and what the form does is change the filter conditions, and now I want to go back to page 1 (which is a different url) – Marion Newlevant Mar 21 '17 at 23:57
  • Ah. In that case setRouteVariables isn't of much use to you, as the data doesn't persist across page requests. Your best bet is to store the data in the session. – Stephen Lewis Mar 22 '17 at 0:09
  • Storing it in a session was the way to go. Do you want to write that up as an answer for me, and I will accept it? – Marion Newlevant Mar 22 '17 at 3:37

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.