Support the ongoing development of Laravel.io →
Requests Session Validation
Last updated 1 year ago.
0

Anyone? I still can't figure this out.

I actually have a different laravel project that it all works fine on. But doing (what I think is) the same thing on this project, and I can't get flashed messages to stay. Only permanent messages into the session work.

The only difference I know for sure is the one it works on is laravel 5.2.14 and the new one, where it doesn't work is 5.2.31.

So either there is a change between the two I i'm not taking into account. Or, more likely, I've got something wrong in my code that I'm too dense to figure out.

0
->withErrors($validator->errors());
0

WrongWay said:

->withErrors($validator->errors());

Nope, still nothing. Its not the object that is the problem I don't think. Its the session not keeping the 'flashed' message.

Its almost like on the redirect, it loading two pages. So the first one (that I don't see) gets the flashed message. And then when the 'second' one loads, the message has been deleted from the session since that is the 2nd request.

Doesn't matter how I flash the message to the session. ->withErrors, ->flash('msg', 'value') or using this

$this->validate($request.....) 

which automatically redirect back if the form field fails validation, I never get the flashed data on the page redirect.

Unless I put it in permanently to the session with ->put('key', 'value'). So sessions are working.

Last updated 7 years ago.
0

So you say there are 2 redirects happening ? It doesn't work this way. Error messages are flashed, not kept

0

Allirght, I think I may have found what's causing my problem. But I'm not sure. And I don't know how to fix it.

I wanted to see if I could watch the session change over time, to see if the variables ever even got placed into the session.

Turns out, my session is being regenerated on EVERY page load. I get lots and lots of sessions.

I made the following route

Route::group(['middleware' => 'web'], function () {
   Route::get ('/test', function () {
      return 'You are here';
   });
};

And if I keep reloading the /test page, it makes a new session on each load. If I move the /test page outside of the web middleware group, and just make it a naked route all by itself, then I only get 1 session created each time I load the page.

I think that may have something to do with my problem. Because my other laravel websites do not create new sessions each page load.

....Edit

After playing around some more. If I delete the following line from the Kernel.php 'web' middleware, then I do NOT get a new session being created on each page refresh. But still I have no flashed session messages being kept between pages.

   \App\Http\Middleware\EncryptCookies::class,

But, I DO get 2 'new' sessions created when I submit a form.

For example:

I have 5 'sessions' files in /storage/framework/sessions.

I submit a form where 1 field is required to be 5 characters, but I only put in 2 letters so it fails validation.

I get redirected back to the form input, with OUT those 2 characters and no error messages from the failed validation. Because like I said above the session is empty of those things.

But now my /storage/framework/sessions folder has 7 sessions in it.

So it seems like its creating multiple sessions. Maybe one for the POST data on form submit and one for the GET data on the redirect???? I don't know.

Last updated 7 years ago.
0

So I couldn't figure this out, so I started over. I'm going to document step by step what I just did, and hopefully someone can tell me if that is expected behavior. Because it seems strange to me.

  1. Install laravel in empty directory

    composer create-project --prefer-dist laravel/laravel site

  2. Changed directory permissions and ownership

sudo chown www-data:www-data -R site/
sudo chmod g+w -R site/
  1. load my site 4 or 5 times, to see how many sessions were being creeated in /storage/framework/sessions Only 1 sesssion was created in the file, despite many reloads

  2. changed my routes.php file from this

Route::get('/', function () {
    return view('welcome');
});

to this

Route::group(['middleware' => 'web'], function () {
    Route::get('/', function () {
        return view('welcome');
    });
});

And now, on each page reload of '/' I get a new session created each time. Dozens of new sessions.

I didn't do anything other than above. I didn't change any .env files. I didn't do any database changes or anything.

My understanding is it should NOT make a new sessions each time you just refresh the page. I've tried refreshing in firefox and chrome, and same thing. New sessions each time.

Is this a bug? Or have I done something wrong? Or is it working as expected and I just misunderstand how often new sessions should be created when part of the 'web' middleware?

0

Found it !!

Apparently in 5.2.27 the routes handling changed. Now ALL routes are added to the web middleware by default.

I did my first laravel 5 project in 5.2.14 where you had to manually assign all routes in the routes.php to the web middleware.

Because 2.31 (after 2.27) automatically adds everything to 'web' and then I was manually adding it to the 'web' middleware, it was creating a new session each time.

So I removed all my routes from the 'web' middleware and just left them in 'auth'. And the web middleware gets applied automatically, and everything works.

Here is where I found my answer in case someone wants more details

https://github.com/laravel/framework/issues/13000

0

@nertskull

Thank you that has been my problem too!

0

Same problem here, thanks

0

Many many thanks! Had the same problem and tried to debug the whole validation process. Thank god it's solved now.

0

Thank you that has been my problem too!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Nertskull nertskull Joined 3 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.