Answers.Body: <p>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 <code>action</code> attribute is empty.</p>
  
  <p>This should work:</p>
  
  <pre><code>public function actionTest() {
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'someVar' =&gt; 23
      ));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-21T23:57:07.713Z
Answers.Text: 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)
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-22T00:09:37.820Z
Answers.Text: 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.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-22T03:37:45.063Z
Answers.Text: 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?
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-21T23:50:23.970Z
Answers.LastActivityDate: 2017-03-21T23:50:23.970Z
Answers.UserDisplayName: Stephen Lewis
Body: <p>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:</p>
  
  <p>Template lives at templates/test.twig:</p>
  
  <pre><code>someVar is defined? {{ someVar is defined ? 'yes' : 'no' }}
  
  &lt;form method="post" action="" accept-charset="UTF-8"&gt;
    {{ getCsrfInput() }}
    &lt;input type="hidden" name="action" value="site/test"&gt;
    &lt;input type="hidden" name="redirect" value="/test"&gt;
    &lt;button type="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Controller code (which does get called):</p>
  
  <pre><code>public function actionTest() {
      craft()-&gt;urlManager-&gt;setRouteVariables(array(
          'someVar' =&gt; 23
      ));
      $this-&gt;redirectToPostedUrl();
  }
  </code></pre>
  
  <p>What I expect, is that when I first visit the page, <code>someVar</code> isn't defined, but when I submit the form and revist the page, <code>someVar</code> will be defined (and equal 23). But it is not defined.</p>
  
Tags: <plugin-development>
Title: redirect to url and set variable in template
ThreadText: # redirect to url and set variable in template
  
  (Asked by: username_0 on 2017-03-21)
  
  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.
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-03-21. Score: 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
      ));
  }
  
  ```
  
  > **Comments:**
  > **username_0**: 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)  
  > **username_1**: 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.  
  > **username_0**: 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?  
  
  ---
  
  