# Flash messages in Slim 4

**URL:** <https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246>\
**Category:** Questions\
**Created:** [June 23, 2020, 8:37pm UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246 "2020-06-23T20:37:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sim](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@sim](https://discourse.slimframework.com/u/sim)\
**Post date:** [June 23, 2020, 8:37pm UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246/1 "2020-06-23T20:37:07Z")

</div>

Hello there!

I try to add a flash message in my controller:

`$this->get('flash')->addMessage('foo', 'bar');`

I get following error message: Call to undefined method App\Controllers\TestController::get().

My route:  
`$group->post('/test', TestController::class . ':register');`

My Container:

```
$container->set('flash', function(){
    return new \Slim\Flash\Messages();
});

```

Can please someone have a look?  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [June 24, 2020, 11:03am UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246/2 "2020-06-24T11:03:27Z")

</div>

`$this` in that context appears to be the `TestController` class which likely doesn’t have a `get` method. You are likely confusing it with a container instance.

What does your TestController class look like?

---

<div class="post-metadata">

**Author:** ![thierryler](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/thierryler/32/1115_2.png) [@thierryler](https://discourse.slimframework.com/u/thierryler)\
**Post date:** [June 24, 2020, 4:07pm UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246/3 "2020-06-24T16:07:03Z")

</div>

If it helps, I wrote that:

```
  private function flashCodes(array $codes, string $key) {
    $allCodes = array_merge(Params::getFromSession($key, []), $codes);
    Params::putInSession($key, $allCodes);
  }

  
  protected function addFlashInfoCodes(array $codes) {
    $this->flashCodes($codes, Constantes::KEY_FLASH_MESSAGES_INFO);
  }

  
   protected function addFlashErrorCodes(array $codes) {
    $this->flashCodes($codes, Constantes::KEY_FLASH_MESSAGES_ERROR);
  }

  
  protected function addFlashData(array $data) {
    $allData = array_merge(Params::getFromSession(Constantes::KEY_FLASH_DATA, []), $data);
    Params::putInSession(Constantes::KEY_FLASH_DATA, $allData);
  }

  protected function getFlashData(string $key, $default = null) {
    $result = $default;
    $flashData = $this->usual->flashData;
    if (is_array($flashData) && isset($flashData[$key])) {
      $result = $flashData[$key];
    }
    return $result;
  }

  protected function flashRedirect(
      Request $request, 
      Response $response, 
      string $routeName, 
      array $routeParams = [], 
      array $errorCodes = [],
      array $infoCodes = [], 
      array $data = [],
      int $httpStatusCode = 302
  ): Response {
    $this->addFlashErrorCodes($errorCodes);
    $this->addFlashInfoCodes($infoCodes);

    $loggerUid = $this->getLoggerUid();

    $data2 = array_merge($data, [
      'loggerUid' => $loggerUid
    ]);
    $this->addFlashData($data2);
    
    return $this->responder->redirectToRoute($request, $response, $routeName, $routeParams, $httpStatusCode);
  }
```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 25, 2020, 6:08am UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246/4 "2020-06-25T06:08:50Z")

</div>

In your case `$this` points to the controller instance. In Slim 4 you may use dependency injection or you use the container instance instead.

---

<div class="post-metadata">

**Author:** ![sim](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@sim](https://discourse.slimframework.com/u/sim)\
**Post date:** [June 25, 2020, 9:25pm UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246/5 "2020-06-25T21:25:19Z")

</div>

My TestController looks like this:

```
namespace App\Controllers;

use App\Exceptions\NotFoundHttpException;
use PDO;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpNotFoundException;
use Slim\Views\Twig;

class TestController{
  protected $view;
  protected $db;

  public function __construct(Twig $view, $db){
    $this->view = $view;
    $this->db = $db;
  }

  public function register(Request $request, Response $response, $args){
    $data = $request->getParsedBody();

     // DB Insert

      $this->get('flash')->addMessage('registered', true);

     //...

  }
}
```

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [June 26, 2020, 11:43am UTC](https://discourse.slimframework.com/t/flash-messages-in-slim-4/4246/6 "2020-06-26T11:43:43Z")

</div>

In that context, `$this` is referring to your `TestController` instance. You will want to bring your instance of Flash into your controller just like you are bringing in your view and the `$db`. Then you can refer to it as `$this->flash->addMessage(...)` instead of $this-\>get(…)`. Something like this (untested).

```php
namespace App\Controllers;

use App\Exceptions\NotFoundHttpException;
use PDO;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpNotFoundException;
use Slim\Flash\Messages as Flash;
use Slim\Views\Twig;

class TestController{
  protected $view;
  protected $db;
  protected $flash;

  public function __construct(Twig $view, $db, Flash flash$){
    $this->view = $view;
    $this->db = $db;
    $this->flash = $flash;
  }

  public function register(Request $request, Response $response, $args){
    $data = $request->getParsedBody();

     // DB Insert

      $this->flash->addMessage('registered', true);

     //...

  }
}
```
