Answers.Body: <p><code>$_SERVER['HTTP_REFERER']</code> should give you the url the request was coming from, which you can then pass in to the redirect function. You could add some URL parameters like <code>?message='Logged%20out!'</code> and have your template check for the message existing and display it. </p>
  
  <p>Be aware that this function comes with a small caveat from the PHP documentation:</p>
  
  <blockquote>
    <p>The address of the page (if any) which referred the user agent to the
    current page. This is set by the user agent. Not all user agents will
    set this, and some provide the ability to modify HTTP_REFERER as a
    feature. In short, it cannot really be trusted.</p>
  </blockquote>
  
  <p>Here's the full <a href="http://php.net/manual/en/reserved.variables.server.php" rel="nofollow">documentation on the PHP $_SERVER array</a>.</p>
  
  <p><strong>Edit: Other Option Using a Form</strong></p>
  
  <p>It might be a little bit clunky, but you would have the like to logout actually be a form submit. You could post the current URL to your controller and use <code>craft()-&gt;urlManager-&gt;setRouteVariables(array('message' =&gt; $myMessageObject));</code> to send your message back to the user.</p>
  
  <p>Form: </p>
  
  <pre><code>&lt;form method="post" action="" accept-charset="UTF-8"&gt;
      &lt;input type="hidden" name="action" value="{{ actionUrl('pluginHandle/controller/method', { return: 'this/page/url' }) }}"&gt;
      &lt;input type="submit" value="{{ 'Log out'|t }}"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>All you'll need is a little bit of CSS to style the submit link, or even somme javascript to trigger the form submit. If you wanted to get even fancier, you could AJAX the request and avoid the form all together. </p>
  
  <p>Here's the <a href="http://buildwithcraft.com/docs/plugins/controllers#posting-to-controller-actions" rel="nofollow">documentation on posting to controller actions</a> for reference.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T06:18:15.000Z
Answers.Text: I was hoping for something more native to craft, where we could pass anything back such as a model. Same was we can with POSTed actions
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T06:27:05.063Z
Answers.Text: I've added an answer that works without using referrer, still not as good as I'd like as it's limited to a string.
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T13:59:53.873Z
Answers.Text: Hey @RussBack - just edited my answer with an alternative solution. I think AJAXing the request would probably be most elegant, but a form works too!
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T02:21:58.870Z
Answers.LastActivityDate: 2015-04-11T13:59:15.210Z
Answers.LastEditDate: 2015-04-11T13:59:15.210Z
Answers.UserDisplayName: Aaron Berkowitz
Answers.Body: <p>I can do this:</p>
  
  <pre><code>&lt;a href="{{ actionUrl('pluginHandle/controller/method', { return: 'this/page/url' }) }}"&gt;Log out&lt;/a&gt;
  </code></pre>
  
  <p>And in my plugin method:</p>
  
  <pre><code>public function actionLogout ($return)
  {
      // do something with an external API here
  
      $this-&gt;redirect("{$return}?message=You have been logged out", $terminate = true, $statusCode = 302);
  }
  </code></pre>
  
  <p>That's OK for a simple message but I was hoping we could pass something back outside of the URL in the same we can with POSTed actions, and also not be limited to what that data is, for example passing back an array or a model instance.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T21:08:16.870Z
Answers.Text: Are you providing an answer to the question? Or is this simply an extension to your original question? If so, I'd recommend **editing** your original question to show your though process so far. Posting it as an answer indicates you're considering it as a viable solution.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-11T06:26:17.797Z
Answers.LastActivityDate: 2015-04-11T06:26:17.797Z
Answers.UserDisplayName: Russ Back
Body: <p>We have a plugin that logs you in and out of a third party service.</p>
  
  <p>Login is easy as we can POST to a controller action, but for logout, we have a link that goes straight to an action URL.</p>
  
  <p>My problem is that I want to redirect back to the URL the user submitted from, with some data that we use in the view (a message to say you've logged out).</p>
  
  <p>redirectToPostedUrl() is no good as this isn't a POST request, and from what I can see in the docs redirect() doesn't give us the option to add data and anything we add in craft()->urlManager->setRouteVariables() doesn't come through when you redirect.</p>
  
  <p>And if we <em>don't</em> redirect or return, we get a 404 as the user is just left on the controller action URL, for which there is no template output.</p>
  
Tags: <plugin-development><routes><redirect>
Title: How can I link to a controller and return/redirect with data?
ThreadText: # How can I link to a controller and return/redirect with data?
  
  (Asked by: username_0 on 2015-04-10)
  
  We have a plugin that logs you in and out of a third party service.
  
  Login is easy as we can POST to a controller action, but for logout, we have a link that goes straight to an action URL.
  
  My problem is that I want to redirect username_0 to the URL the user submitted from, with some data that we use in the view (a message to say you've logged out).
  
  redirectToPostedUrl() is no good as this isn't a POST request, and from what I can see in the docs redirect() doesn't give us the option to add data and anything we add in craft()->urlManager->setRouteVariables() doesn't come through when you redirect.
  
  And if we *don't* redirect or return, we get a 404 as the user is just left on the controller action URL, for which there is no template output.
  
  **Tags:** plugin-development, routes, redirect
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-04-11. Score: 1
  
  `$_SERVER['HTTP_REFERER']` should give you the url the request was coming from, which you can then pass in to the redirect function. You could add some URL parameters like `?message='Logged%20out!'` and have your template check for the message existing and display it.
  
  Be aware that this function comes with a small caveat from the PHP documentation:
  
  > The address of the page (if any) which referred the user agent to the
  > current page. This is set by the user agent. Not all user agents will
  > set this, and some provide the ability to modify HTTP\_REFERER as a
  > feature. In short, it cannot really be trusted.
  
  Here's the full [documentation on the PHP $\_SERVER array](http://php.net/manual/en/reserved.variables.server.php).
  
  **Edit: Other Option Using a Form**
  
  It might be a little bit clunky, but you would have the like to logout actually be a form submit. You could post the current URL to your controller and use `craft()->urlManager->setRouteVariables(array('message' => $myMessageObject));` to send your message username_0 to the user.
  
  Form:
  
  ```
  <form method="post" action="" accept-charset="UTF-8">
      <input type="hidden" name="action" value="{{ actionUrl('pluginHandle/controller/method', { return: 'this/page/url' }) }}">
      <input type="submit" value="{{ 'Log out'|t }}">
  </form>
  
  ```
  
  All you'll need is a little bit of CSS to style the submit link, or even somme javascript to trigger the form submit. If you wanted to get even fancier, you could AJAX the request and avoid the form all together.
  
  Here's the [documentation on posting to controller actions](http://buildwithcraft.com/docs/plugins/controllers#posting-to-controller-actions) for reference.
  
  > **Comments:**
  > **username_0**: I was hoping for something more native to craft, where we could pass anything username_0 such as a model. Same was we can with POSTed actions  
  > **username_0**: I've added an answer that works without using referrer, still not as good as I'd like as it's limited to a string.  
  > **username_1**: Hey @username_0 - just edited my answer with an alternative solution. I think AJAXing the request would probably be most elegant, but a form works too!  
  
  ---
  
  ### Answer by username_0 on 2015-04-11. Score: 0
  
  I can do this:
  
  ```
  <a href="{{ actionUrl('pluginHandle/controller/method', { return: 'this/page/url' }) }}">Log out</a>
  
  ```
  
  And in my plugin method:
  
  ```
  public function actionLogout ($return)
  {
      // do something with an external API here
  
      $this->redirect("{$return}?message=You have been logged out", $terminate = true, $statusCode = 302);
  }
  
  ```
  
  That's OK for a simple message but I was hoping we could pass something username_0 outside of the URL in the same we can with POSTed actions, and also not be limited to what that data is, for example passing username_0 an array or a model instance.
  
  > **Comments:**
  > **username_2**: Are you providing an answer to the question? Or is this simply an extension to your original question? If so, I'd recommend **editing** your original question to show your though process so far. Posting it as an answer indicates you're considering it as a viable solution.  
  
  ---
  
  