<p>You can set <a href="https://www.yiiframework.com/doc/guide/2.0/en/runtime-sessions-cookies#flash-data" rel="noreferrer">flash messages</a> using <a href="https://www.yiiframework.com/doc/api/2.0/yii-web-session#setFlash()-detail" rel="noreferrer">yii\web\Session::setFlash()</a>.</p>

<p>The <code>session</code> component is available to templates via <code>craft.app.session</code>.</p>



<pre><code>{% do craft.app.session.setFlash('error', 'You’re not allowed to go there.') %}
</code></pre>

<p>For convenience, Craft’s <a href="https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html" rel="noreferrer">SessionBehavior</a> class adds shortcut <a href="https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html#method-seterror" rel="noreferrer">setError()</a> and <a href="https://docs.craftcms.com/api/v3/craft-behaviors-sessionbehavior.html#method-setnotice" rel="noreferrer">setNotice()</a> methods to the <code>session</code> component as well.</p>

<pre><code>{% do craft.app.session.setError('You’re not allowed to go there.') %}
</code></pre>

<p>You can place that right before your <code>{% redirect %}</code> tag.</p>

CC BY-SA 4.0
2019-01-15T00:16:38.917Z
2019-01-15T00:16:38.917Z
Brandon Kelly
<p><strong>Edit: Brandon replied as I was writing this. Feel free to remove :)</strong></p>

<p>You can do that using this in your conditional, just above your redirect tag</p>

<pre><code>{% do craft.app.session.setFlash('error', 'no no no') %}
{% redirect "tosomewhere" %}
</code></pre>

<p>Then in the template you are redirecting to</p>

<pre><code>{% set message = craft.app.session.getFlash('error', null, true) %}
{% if message %}
    {{ message }}
{% endif %}
</code></pre>

CC BY-SA 4.0
2019-01-15T00:23:34.023Z
2019-01-15T00:29:03.663Z
2019-01-15T00:29:03.663Z
Oli
<p>How does one set a flash message in the templates? I can't find any documentation about this, only for plugins or getting the message. </p>

<p>My use-case is, I am checking if a user is allowed to access a certain page and if they cannot I want to set a flash message, then redirect them to another page. I prefer this than to keep them on the same page and is if else logic to show the message or the rest of the page. </p>

<p>Thanks</p>

CC BY-SA 4.0
2019-01-14T09:09:09.667Z
AFAIK it is not possible to set a flash message from the template, however this might be a nice improvement for the `{% redirect %}` tag.
Oliver Stark
CC BY-SA 4.0
2019-01-15T23:52:01.650Z
Now (Craft 3.1) you can set a notice/error message with the `{% redirect %}` tag: https://docs.craftcms.com/v3/dev/tags/redirect.html#flash-messages
Oliver Stark
CC BY-SA 4.0
2019-01-16T10:51:25.940Z
redirect with flash notice is great. I had not found that documentation because its within the redirect documentation, whereas I had been searching for flash and did not find this page! Thanks.
Laurence Cope
CC BY-SA 4.0
2019-01-17T08:59:53.523Z
Laurence, it was added to the docs yesterday, along with the Craft 3.1 release ;-) https://github.com/craftcms/cms/pull/3625
Oliver Stark
<craft3>
How do you set a flash message in the template in Craft 3?
# How do you set a flash message in the template in Craft 3?

(Asked by: username_0 on 2019-01-12)

How does one set a flash message in the templates? I can't find any documentation about this, only for plugins or getting the message.

My use-case is, I am checking if a user is allowed to access a certain page and if they cannot I want to set a flash message, then redirect them to another page. I prefer this than to keep them on the same page and is if else logic to show the message or the rest of the page.

Thanks

**Tags:** craft3
> **Comments:**
> **username_1**: AFAIK it is not possible to set a flash message from the template, however this might be a nice improvement for the `{% redirect %}` tag.  
> **username_1**: Now (Craft 3.1) you can set a notice/error message with the `{% redirect %}` tag: https://docs.craftcms.com/v3/dev/tags/redirect.html#flash-messages  
> **username_0**: redirect with flash notice is great. I had not found that documentation because its within the redirect documentation, whereas I had been searching for flash and did not find this page! Thanks.  
> **username_1**: Laurence, it was added to the docs yesterday, along with the Craft 3.1 release ;-) https://github.com/craftcms/cms/pull/3625  

---

## 2 answers

### Answer by username_2 on 2019-01-15. Score: 13

You can set flash messages using yii\web\Session::setFlash().
The session component is available to templates via craft.app.session.
{% do craft.app.session.setFlash('error', 'You’re not allowed to go there.') %}

For convenience, Craft’s SessionBehavior class adds shortcut setError() and setNotice() methods to the session component as well.
{% do craft.app.session.setError('You’re not allowed to go there.') %}

You can place that right before your {% redirect %} tag.


---

### Answer by username_3 on 2019-01-15. Score: 10

Edit: Brandon replied as I was writing this. Feel free to remove :)
You can do that using this in your conditional, just above your redirect tag
{% do craft.app.session.setFlash('error', 'no no no') %}
{% redirect "tosomewhere" %}

Then in the template you are redirecting to
{% set message = craft.app.session.getFlash('error', null, true) %}
{% if message %}
    {{ message }}
{% endif %}


---

