Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JohnnyGan's avatar

Session::all() is empty after Session data saved.

I tried to do very a simple form submission, and save submitted form data into session, then display saved data into same form.

.env

CACHE_DRIVER=memcached
SESSION_DRIVER=memcached
QUEUE_DRIVER=database

and in bootstrap/app.php

Dotenv::load(__DIR__.'/../');
...
$app->withFacades();

 $app->middleware([
      'Illuminate\Session\Middleware\StartSession',
 ]);

Assume I could use Session::get and Session::put in the code.

I created routes.php

//testAPIs using for API post methods testing
$app->get('/testAPIs',
    ['as'=>'testapi',
        'uses'=>'App\Http\Controllers\SystemController@testAPIs']
);
$app->post('/postAPIs',
    ['uses'=>'App\Http\Controllers\SystemController@handleAPIs']
);

Then in SystemController.php

  /*
     * Test APIs form and Initial data
     *
     * @param NULL
     * @return $data, System information data
     */
    public function testAPIs()
    {

        if(Session::has('testAPI-response'))
        {
            $response=Session::get('testAPI-response');
        }
        else
        {
            $response="NULL";
        }

        if(Session::has('testAPI-request'))
        {
            $request=Session::get('testAPI-request');
        }
        else
        {
            $request="NULL";
        }

        return view('testapi',compact('response','request'));
    }

    /**
     * Handle the incoming API post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function handleAPIs(Request $request)
    {
        Session::put("testAPI-request", $request->input('request'));
        Session::put("testAPI-response", $request->input('request'));
        return redirect()->route('testapi');
    }

And testapi.blade.php just two fields with a submit button

<div class="container">
    <div class="h2">API Test Client</div>
    <form method="POST" action="/postAPIs" accept-charset="UTF-8"><input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <table class="table-condensed">
            <tr>
                <td>
                    <table class="table-condensed">
                        <tr>
                            <td colspan="6"><label class="strong media-body" for="response">Request Data (In JSON format): </label></td>
                        </tr>
                        <tr>
                            <td colspan="6">
                                <textarea name="request" rows="6" cols="100">{{ $request }}</textarea>
                            </td>
                        </tr>
                    </table>
                    <p class="text-center" >
                        <input class="btn btn-primary" type="submit" value="Run!">
                    </p>
                    <table class="table-condensed">
                        <tr>
                            <td><label class="strong media-body" for="response">Response: {{ $responseTime }}</label></td>
                        </tr>
                        <tr>
                            <td colspan="5">
                                <textarea rows="12" cols="100">{{ $response }}</textarea>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

Each time, in handleAPIs before redirect()->route('testapi'); I can see there is data in Session::all(), but in testAPIs, if(Session::has('testAPI-response')) always false, so no data saved in Session.

Is that something wrong in my configuration, because I could not catch any error in the error log.

Thanks in advanced.

0 likes
3 replies
JohnnyGan's avatar

I just update to Lumen 5.0.10, the code was working in 5.0.9. So some changes recently break the Session?

JohnnyGan's avatar

Continue debugging, and found csrf_token is not in Session flash any more.

JohnnyGan's avatar
JohnnyGan
OP
Best Answer
Level 1

After I pull back to lumen-framework 5.0.9, the issue fixed. Something wrong in 5.0.10 Session part.

Please or to participate in this conversation.