Laravel wrong domain in reset password link

2019-07-23 14:11发布

问题:

I'm getting a bad URL generated by sendResetLinkResponse controller in laravel 5.8 having the domain twice.

https://api.domain.org/domain.org/password/reset/....

But it should be

https://api.domain.org/password/reset/....

The APP_URL is set to

APP_URL=domain.org

I use a custom config to be able to have as endpoint api.domain.org instead of www.domain.org/api

My configuration is:

protected function mapApiRoutes() {

 Route::domain('api.' .  env('APP_URL'))
   ->middleware('api')
   ->namespace($this->namespace)
   ->group(base_path('routes/api.php'));
}

How can I fix it?

回答1:

I would suggest setting up your subdomain to be more dynamic i.e.

Route::domain('api.{domain}')
    ->middleware(['api', function ($request, $next) {
        $request->route()->forgetParameter('domain');

        return $next($request);
    }])
    ->namespace($this->namespace)
    ->group(base_path('routes/api.php'));

The above basically allows for any domain name and then the middleware just removes it from your route params so that it doesn't mess with your route closures or controller methods.

You will also need to add the following to the boot method of your service provider:

Route::pattern('domain', '[a-z0-9.]+');

This way you can use the APP_URL to just be the domain for the site.