<p>You could do this by saving the email address to a custom session variable like this in your controller:</p>

<pre><code>Craft::$app-&gt;getSession()-&gt;set('email', $email);
</code></pre>

<p>This should then be available in your template using:</p>

<pre><code>{{ set email = craft.app.session.get('email') }}
</code></pre>

CC BY-SA 4.0
2019-10-15T11:47:50.657Z
I did try that but for some reason it wasn't working. I'll give it another go.
foamcow
CC BY-SA 4.0
2019-10-15T12:44:41.277Z
Well it worked that time with a minor change to `Craft::$app->getSession()->set('email',$email)`. Would still like to know if there's a way to do it without setting the session variable.
foamcow
CC BY-SA 4.0
2019-10-15T13:28:04.587Z
The only way to do it would be either to set a session variable or to pass GET variables to the redirect destination
Cole Henley
CC BY-SA 4.0
2019-10-15T11:35:31.330Z
2019-10-15T13:31:44.263Z
2019-10-15T13:31:44.263Z
Steve Rowling
<p>I am building a custom login process which follow these steps:</p>

<ol>
<li>Request user email (submits to custom controller)</li>
<li>Check if user has an account</li>
<li>If they have an account, show a form requesting their password. Or if they don't have an account then send them to the registration form.</li>
</ol>

<p>The forms exist as front end templates.</p>

<p>I can accomplish all these steps but when the user is redirected I need to make the email address that they entered available to the template.</p>

<p>My custom controller calls a service to check if the user exists which is all fine.
And I can redirect the user to the appropriate URL. But I can't figure out how to retain the email address they entered.</p>

<p>Currently I've just done it by appending the email as a query string but this is undesirable for a couple of reasons.</p>

<p>The controller currently looks like this although I feel this is totally the wrong way to be doing things.</p>

<pre><code>public function actionEmailCheck() {
        $this-&gt;requirePostRequest();

        $email = Craft::$app-&gt;request-&gt;post('loginName');

        $result = MyModule::$instance-&gt;order-&gt;checkEmail($email);

        if($result) {
            $redirect = '/login/password?email='.$email;
        } else {
            $redirect = '/register?email='.$email;
        }

        $this-&gt;redirect($redirect);
    }
</code></pre>

<p>I feel like I should be able to set some template variables and load the appropriate template but that doesn't seem to work.</p>

<p>I also tried this</p>

<pre><code>if($result) {
            $this-&gt;renderTemplate('/login/password', array('email'=&gt;$email));
        } else {
            $this-&gt;renderTemplate('/register', array('email'=&gt;$email));
        }
</code></pre>

<p>But while the template was displayed, I couldn't access the value for 'email'.</p>

<p>So how <em>should</em> I approach this?</p>

<plugin-development><redirect><variable><modules>
Redirect to template from controller and pass data
# Redirect to template from controller and pass data

(Asked by: username_0 on 2019-10-15)

I am building a custom login process which follow these steps:

Request user email (submits to custom controller)
Check if user has an account
If they have an account, show a form requesting their password. Or if they don't have an account then send them to the registration form.

The forms exist as front end templates.
I can accomplish all these steps but when the user is redirected I need to make the email address that they entered available to the template.
My custom controller calls a service to check if the user exists which is all fine.
And I can redirect the user to the appropriate URL. But I can't figure out how to retain the email address they entered.
Currently I've just done it by appending the email as a query string but this is undesirable for a couple of reasons.
The controller currently looks like this although I feel this is totally the wrong way to be doing things.
public function actionEmailCheck() {
        $this->requirePostRequest();

        $email = Craft::$app->request->post('loginName');

        $result = MyModule::$instance->order->checkEmail($email);

        if($result) {
            $redirect = '/login/password?email='.$email;
        } else {
            $redirect = '/register?email='.$email;
        }

        $this->redirect($redirect);
    }

I feel like I should be able to set some template variables and load the appropriate template but that doesn't seem to work.
I also tried this
if($result) {
            $this->renderTemplate('/login/password', array('email'=>$email));
        } else {
            $this->renderTemplate('/register', array('email'=>$email));
        }

But while the template was displayed, I couldn't access the value for 'email'.
So how should I approach this?

**Tags:** plugin-development, redirect, variable, modules

---

## 1 answers

### Answer by username_1 on 2019-10-15. Score: 1 | Accepted answer

You could do this by saving the email address to a custom session variable like this in your controller:
Craft::$app->getSession()->set('email', $email);

This should then be available in your template using:
{{ set email = craft.app.session.get('email') }}

> **Comments:**
> **username_0**: I did try that but for some reason it wasn't working. I'll give it another go.  
> **username_0**: Well it worked that time with a minor change to `Craft::$app->getSession()->set('email',$email)`. Would still like to know if there's a way to do it without setting the session variable.  
> **username_2**: The only way to do it would be either to set a session variable or to pass GET variables to the redirect destination  

---

