Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Flash messages middleware failed. Session not found" when using default Slim test runner #27

Closed
kevinburke opened this issue Dec 1, 2016 · 4 comments

Comments

@kevinburke
Copy link

Hi, I set up my application using the skeleton here: https://github.com/slimphp/Slim-Skeleton

Specifically I'm using this test runner to exercise controllers: https://github.com/slimphp/Slim-Skeleton/blob/master/tests/Functional/BaseTestCase.php

I defined a controller that says $this->flash->addMessage('Error', 'Invalid phone number')

I get the following stack trace:

Slim Application Error:
Type: RuntimeException
Message: Flash messages middleware failed. Session not found.
File: /Users/kevin/code/callyo-10-21-commander/vendor/slim/flash/src/Messages.php
Line: 62
Trace: #0 /Users/kevin/code/callyo-10-21-commander/web/src/dependencies.php(37): Slim\Flash\Messages->__construct()
#1 /Users/kevin/code/callyo-10-21-commander/vendor/pimple/pimple/src/Pimple/Container.php(113): Tests\Functional\BaseTestCase->{closure}(Object(Slim\Container))
#2 /Users/kevin/code/callyo-10-21-commander/vendor/slim/slim/Slim/Container.php(123): Pimple\Container->offsetGet('flash')
#3 /Users/kevin/code/callyo-10-21-commander/vendor/slim/slim/Slim/Container.php(172): Slim\Container->get('flash')
#4 /Users/kevin/code/callyo-10-21-commander/web/src/controllers/GroupsController.php(34): Slim\Container->__get('flash')
#5 [internal function]: Commander\Controllers\GroupsController->getList(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#6 /Users/kevin/code/callyo-10-21-commander/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#7 /Users/kevin/code/callyo-10-21-commander/vendor/slim/slim/Slim/Route.php(344): Slim\Handlers\Strategies\RequestResponse->__invoke(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#8 /Users/kevin/code/callyo-10-21-commander/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(116): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 /Users/kevin/code/callyo-10-21-commander/vendor/slim/slim/Slim/Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))

Not sure whether the error is in Slim's test runner or here, but it seems like this should work without blowing up.

It's also worth considering whether the default should be to fail silently instead of loudly.

@uyab
Copy link

uyab commented Feb 10, 2017

To avoid $_SESSION, you can pass array [] or other object that implement ArrayAccess to Slim\Flash\Messages constructor.

Here is snippet of mine:

// Flash messages
$container['flash'] = function ($c) {
    $driver = \App\Factories\SessionFactory::create($c->get('settings')['session']['driver']);

    return new Slim\Flash\Messages($driver);
};

// Session Factory
class SessionFactory
{
    public static function create($driver)
    {
        switch ($driver) {
            case 'array':
                return [];
            case 'native':
            default:
                return null;
        }
    }
}

// settings.php
        'session' => [
            'driver' => getenv('SESSION_DRIVER'),
        ],

@pedroalpha
Copy link

I've got that problem too!

@akrabat
Copy link
Member

akrabat commented Oct 22, 2017

Start the session with session_start() before you use Slim-Flash.

@akrabat akrabat closed this as completed Oct 22, 2017
@madsem
Copy link

madsem commented Jul 19, 2018

@akrabat I have the same issue, session_start is called and also everything works on existing routes.
But on non-existing routes I get the same error, I was expecting that slim simply returns a NotFoundException for these cases, but somehow flash message doesn't play nice.

For example I visit http://dev.vm/admin/login and everything works fine
Visiting a route that is expected to throw a 404 like http://dev.vm/sfsdsd/dsfdsfds throws the below error:

Slim Application Error
The application could not run because of the following error:

Details
Type: RuntimeException
Message: Flash messages middleware failed. Session not found.
File: /vagrant/klix-landers/vendor/slim/flash/src/Messages.php
Line: 66
Trace
#0 /vagrant/klix-landers/app/Providers/FlashMessageServiceProvider.php(34): Slim\Flash\Messages->__construct()
#1 [internal function]: Klever\Providers\FlashMessageServiceProvider->Klever\Providers\{closure}()
#2 /vagrant/klix-landers/vendor/league/container/src/Definition/CallableDefinition.php(21): call_user_func_array(Object(Closure), Array)
#3 /vagrant/klix-landers/vendor/league/container/src/Container.php(290): League\Container\Definition\CallableDefinition->build()
#4 /vagrant/klix-landers/vendor/league/container/src/Container.php(90): League\Container\Container->getFromThisContainer('flash', Array)
#5 /vagrant/klix-landers/app/Providers/ViewServiceProvider.php(70): League\Container\Container->get('flash')
#6 [internal function]: Klever\Providers\ViewServiceProvider->Klever\Providers\{closure}()
#7 /vagrant/klix-landers/vendor/league/container/src/Definition/CallableDefinition.php(21): call_user_func_array(Object(Closure), Array)
#8 /vagrant/klix-landers/vendor/league/container/src/Container.php(290): League\Container\Definition\CallableDefinition->build()
#9 /vagrant/klix-landers/vendor/league/container/src/Container.php(90): League\Container\Container->getFromThisContainer('view', Array)
#10 /vagrant/klix-landers/bootstrap/helpers.php(105): League\Container\Container->get('view')
#11 /vagrant/klix-landers/app/Providers/NotFoundHandlerServiceProvider.php(35): view('errors/404.twig')
#12 /vagrant/klix-landers/vendor/slim/slim/Slim/App.php(528): Klever\Providers\NotFoundHandlerServiceProvider->Klever\Providers\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 /vagrant/klix-landers/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#14 /vagrant/klix-landers/vendor/slim/slim/Slim/App.php(406): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#15 /vagrant/klix-landers/vendor/slim/slim/Slim/App.php(314): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#16 /vagrant/klix-landers/public/index.php(10): Slim\App->run()
#17 {main}

Am I supposed to create a 404Controller or something to handle this? Or is this unexpected behavior?

I register flash message like this in my ViewServiceProvider:

// add \Slim\Flash messages
$engine->getEnvironment()->addGlobal('flash', $this->getContainer()->get('flash'));

While typing, I think I figured out why this happens, but not how to solve it...
I am running session_start() as Middleware, assigned to specific route groups, because I want to customize the admin session ttl and for guests (it's a tracking system, so guests session should expire very shortly)

That probably means that 404 routes have no session_start() called.

Is that something flash messages should be aware of, as in if an exception was thrown or is that on developers end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants