I am encountering the following error in my new installation of laravel 11
and vue
when I try to submit a form, in this case, the Register form.
419 | PAGE EXPIRED
I have checked this question and yes, I do have @csrf
in my register form and
<meta name="csrf-token" content="{{ csrf_token() }}">
Steps to reproduce
- I installed a fresh instance of
laravel 11
:
composer create-project laravel/laravel test
cd test/
- I added the
laravel/ui
package
composer require laravel/ui
- I added the vue and auth ui
php artisan ui vue --auth
It gave me a warning:
The [Controller.php] file already exists. Do you want to replace it? (yes/no) [yes]
I typed yes
then [Enter]
- I installed the
npm
packages and initializedvite
npm install && npm run dev
- Iniatilized the dev server and tried to register
php artisan serve
Edit
There are additional steps that were done on the same repo
. We work with mongodb
hence we added the Mongodb configurations as well.
- Installed the
laravel/mongodb
package
composer require mongodb/laravel-mongodb
- Added the configurations in
config/database.php
as below:
...
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'assets'),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'options' => [
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
],
],
...
- Updated .env file as shown below:
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=testdb
DB_USERNAME=
DB_PASSWORD=
...
I am hoping there’s someone with a solution.
NB I tried to install laravel 10
and do the same processes and it works perfectly. What has changed in laravel 11
? Any help is appreciated.
Edit
Below is the contents of my bootstrap/app.php
<?php
use IlluminateFoundationApplication;
use IlluminateFoundationConfigurationExceptions;
use IlluminateFoundationConfigurationMiddleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
and register.blade.php
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="row mb-3">
// Rest of code
2
Answers
Although the question doesn’t explicitly mention it, I believe you’ve switched the default database to MongoDB as well.
Since this was set up this way after installation, it may be necessary to regenerate the table for the Session.
In Laravel 11, this command would have been automatically executed by
php artisan migrate
, but it was performed before installing MongoDB. I assume that after migrating to the new database, you would need to rerun themigrate
command separately. Otherwise, only the session table can be created manually.More information
It seems like you’re using the
database
driver as your session driver. This is the default since Laravel 11.I’m sure that this problem has something to do with MongoDB, but I would suggest you to use the
file
session driver, you can do this by setting theSESSION_DRIVER
variable inside your.env
file tofile
:Eitherway, in your case I don’t see why you would need to use a database to store your sessions. Using filesystem is much faster and has way smaller overhead.