# Accessing Flash messages in Middleware

**URL:** <https://discourse.cakephp.org/t/accessing-flash-messages-in-middleware/10711>\
**Category:** Need Help\
**Created:** [October 25, 2022, 9:04am UTC](https://discourse.cakephp.org/t/accessing-flash-messages-in-middleware/10711 "2022-10-25T09:04:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [October 25, 2022, 9:04am UTC](https://discourse.cakephp.org/t/accessing-flash-messages-in-middleware/10711/1 "2022-10-25T09:04:59Z")

</div>

I’m using CakePHP 4. In Middleware I would like to access Flash message, but it’s always empty.

In Middleware I’m using this code:

```auto
$session = $request->getSession();
debug($session->read('Flash'));

```

As I’m aware Flash messages are saved in session. I’m doing something wrong? On site I see flash message…

Tnx

---

<div class="post-metadata">

**Author:** ![mvojwe](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mvojwe](https://discourse.cakephp.org/u/mvojwe)\
**Post date:** [October 25, 2022, 10:23am UTC](https://discourse.cakephp.org/t/accessing-flash-messages-in-middleware/10711/2 "2022-10-25T10:23:26Z")

</div>

What are you trying to achieve? according to [this](https://book.cakephp.org/authorization/2/en/middleware.html#how-to-create-a-custom-unauthorizedhandler) you can do something like this:

```auto
if ($request instanceof ServerRequest) {
$request->getFlash()->error(__('Error'));
}

```

I use the if clause so I dont get annoyed by the vscode Intellisense

Also, according to [this](https://book.cakephp.org/4/en/controllers/components/flash.html#setting-flash-messages) you can start a new stack with:

```auto
$this->Flash->success('The user has been saved', ['clear' => true]);

```
