Home > database >  Laravel Notifications not responding to APP_URL changes
Laravel Notifications not responding to APP_URL changes

Time:11-02

I am testing my password reset notification to see if it responds to changes in the APP_URL, unfortunately it does not. It responds to changes in the APP_NAME env variable nonetheless. I cleared my cache, I cleared my views, I ran php artisan optimize:clear and unfortunately nothing worked.

What is quite strange is that it responds to changes in the APP_NAME not the APP_URL ...

This is my env file

APP_NAME=WP
APP_ENV=local
APP_KEY=base64:uqkPjWhwt7orbinLRlRN BNn6BxbdIRHSV4dG4dw5S0=
APP_DEBUG=true
APP_URL=http://example.com

This is the ResetPassword toMail() function which is unaltered

    public function toMail($notifiable)
{
    if (static::$toMailCallback) {
        return call_user_func(static::$toMailCallback, $notifiable, $this->token);
    }

    if (static::$createUrlCallback) {
        $url = call_user_func(static::$createUrlCallback, $notifiable, $this->token);
    } else {
        $url = url(route('password.reset', [
            'token' => $this->token,
            'email' => $notifiable->getEmailForPasswordReset(),
        ], false));
    }

    //dd(env('APP_URL')); returns http://example.com
    // dd(config('app.url')); returns http://example.com
    // dd($url); returns http://127.0.0.1:8000/{token}?email!




    return (new MailMessage)
        ->subject(Lang::get('Reset Password Notification'))
        ->line(Lang::get('You are receiving this email because we received a password reset request for your account.'))
        ->action(Lang::get('Reset Password'), $url)
        ->line(Lang::get('This password reset link will expire in :count minutes.', ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')]))
        ->line(Lang::get('If you did not request a password reset, no further action is required.'));
}

CodePudding user response:

Queued notifications will use the APP_URL config value when building urls. However, non-queued notifications will use the HTTP request information when building urls.

The ResetPassword notification is not a queued notification, so it will use the url from the original HTTP request that triggered the notification. In this case, it looks like you're testing locally at http://127.0.0.1:8000, so that is the base url that will be used to build the password reset link.

If the notification was queued, then the notification would be sent using a queue worker that runs the console kernel. The console kernel builds a fake request, which sets the base url based on the APP_URL config value.


Note, there was a period of time where the ResetPassword notification specifically used config('app.url') in the notification to build the URL. This was introduced in 5.4.22, but later removed in 6.18.7. Since then, it has behaved like every other notification.

CodePudding user response:

You might need to run php artisan cache:clear and php artisan config:clear after making changes to your .env file.

https://laravel.com/docs/6.x/configuration#configuration-caching

  • Related