Home > database >  Undefined variable $request after moving to VPS
Undefined variable $request after moving to VPS

Time:12-28

I have a Laravel 9 application successfully running in a shared hosting service. I recently tried migrating the site to a VPS because I needed more resources but I get the following error in one of the views:

Undefined variable $request (View: /var/www/html/resources/views/auth/reset-password.blade.php)

I am using the Laravel resetting passwords system and it works fine in the shared hosting but in the VPS every time I try to reset the password I get the error mentioned above

In the web.php file I have the same routes as defined in the laravel resetting passwords site:

Route::get('/reset-password/{token}', function ($token) {
    return view('auth.reset-password', ['token' => $token]);
})->middleware('guest')->name('password.reset');

Route::post('/reset-password', function (Request $request) {
    $request->validate([
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|min:8|confirmed',
    ]);
 
    $status = Password::reset(
        $request->only('email', 'password', 'password_confirmation', 'token'),
        function ($user, $password) {
            $user->forceFill([
                'password' => Hash::make($password)
            ])->setRememberToken(Str::random(60));
 
            $user->save();
 
            event(new PasswordReset($user));
        }
    );
 
    return $status === Password::PASSWORD_RESET
                ? redirect()->route('login')->with('status', __($status))
                : back()->withErrors(['email' => [__($status)]]);
})->middleware('guest')->name('password.update');

And in the view, I have modified the style but I am using the same content as the default reset-password Laravel view, the line that doesn't work properly in the VPS is:

<input type="hidden" name="token" value="{{ $request->route('token') }}">

I am sure I am using exactly the same code (Except the .env file, it was modified with the new database credentials) in the shared hosting and the VPS because I have it in a git repository.

I have already executed the following commands:

npm run prod

php artisan config:clear
php artisan route:clear
php artisan view:clear

Why is the $request variable not found in the VPS but it works properly in the shared hosting?

CodePudding user response:

This is honestly not a good approach, you should research more on how controllers & models work, doing all of your coding inside of the route is not recommended - honestly it isn't your fault, as you can find the example in the documentation, which should be improved.

I recommend you take a look at https://laravel.com/docs/9.x/starter-kits#laravel-breeze it does already all of the authentication for you, and you can opt to choose which works best for you, test it out on your localhost first to get familiar with it. I recommend to use Breeze with Blade as a beginner: https://laravel.com/docs/9.x/starter-kits#breeze-and-blade

Meanwhile to fix your issue:

Change:

<input type="hidden" name="token" value="{{ $request->route('token') }}">

To:

<input type="hidden" name="token" value="{{ $token }}">

Reason:

When you pass the token or any variable to the view like this:

return view('auth.reset-password', ['token' => $token]);

you can use it directly in the blade without calling request with {{ $token }}

  • Related