<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>

CC BY-SA 3.0
2015-04-11T06:18:15.000Z
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
Russ Back
CC BY-SA 3.0
2015-04-11T06:27:05.063Z
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.
Russ Back
CC BY-SA 3.0
2015-04-11T13:59:53.873Z
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!
Aaron Berkowitz
CC BY-SA 3.0
2015-04-11T02:21:58.870Z
2015-04-11T13:59:15.210Z
2015-04-11T13:59:15.210Z
Aaron Berkowitz
<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>

CC BY-SA 3.0
2015-04-11T21:08:16.870Z
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.
Lindsey D
CC BY-SA 3.0
2015-04-11T06:26:17.797Z
2015-04-11T06:26:17.797Z
Russ Back
<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>

<plugin-development><routes><redirect>
How can I link to a controller and return/redirect with data?
# 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 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).

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.
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 back to the user.
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 for reference.

> **Comments:**
> **username_0**: 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  
> **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 @RussBack - 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:
Log out

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 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.

> **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.  

---

