URL generator putting “localhost” before domain in Laravel

眉间皱痕 提交于 2020-06-01 05:14:55

问题


I've been trying to email users a verification link using signed URLs in Laravel. It seems to work okay, but the link comes out incorrect when it is built using the URL facade.

public function toMail($notifiable)
{
    $url = URL::signedRoute('confirm', ['user' => $this->user->id]);

    return (new MailMessage)
                ->subject('Activate your email address')
                ->line('In order to use the application, please verify your email address.')
                ->action('Activate your account', $url)
                ->line('Thank you for using our application!');
}

In the email, the link looks like:

http://localhost/mydomain.com/confirm/14?signature=3ba4d86827717440f70a3b2f60c913b6e84d550cb9fce8de04a8ba359833ac7c

The "localhost" part should not be there. However, if I manually delete it in the URL bar, I believe the signed URL things I manipulated the URL and gives me a 401 error. I am running on a localhost environment but I use Laragon's auto virtual host so that I can still run it with a domain.

Any suggestions?


回答1:


Change:

APP_URL=example.com

To:

APP_URL=http://example.com

I guess not specifying "http://" makes it append localhost to the front. Hope this helps someone!




回答2:


Sometimes if you are working in virtual environments or docker container, just setting APP_URL won't work. Try the following steps.

Step 1: Set your domain in your .env file. (Don't forget the double quotes)

APP_URL="http://yourdomain.com"

Step 2: Add following line before generating your signed route

URL::forceRootUrl(\config('app.url'));

Step 3: (Optional) Add following line, if you want to force https scheme

URL::forceScheme('https');


来源:https://stackoverflow.com/questions/54032810/url-generator-putting-localhost-before-domain-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!