简体   繁体   中英

Laravel: Change app url in routes called inside queued notifications

I'm developing a tenant-based application and want to customized notifications for each tenant. My problem is that once Laravel queue starts running, it reads APP_URL=olddomain.com environment variable and use it for each route generated inside notifications. Even if I try to change the config using config(['app.url' => 'newdomain.com']) , it will still output any route with olddomain.com prefix. I can pass newdomain.com as a parameter to the notification and do something like this:

$domain = "newdomain.com"; 
$route = route('named-route', [{parameters}], false);
$finalRoute = $domain . '/' . $route; (Output: newdomain.com/named-route)

But this seems too "hacky". Is it possible to change app.url for each individual notification in queue?

you can do this, put this in your 'app/Providers/AppServiceProvider.php' inside of your boot method put this:

app()->singleton('new-domain', 'https://newdomain.com');

then, execute php artisan config:cache in your terminal (in your project folder) and then call it wherever you want with this:

$myCustomizedDomain = app('new-domain');
$myCustomizedUrl = $myCustomizedDomain . "/my-prefix";

Because there is no way to do what you are looking for, sorry.

EDIT:

I found this in the laravel documentation version-6x:

  • Redirecting To External Domains

Sometimes you may need to redirect to a domain outside of your application. You may do so by calling the away method, which creates a RedirectResponse without any additional URL encoding, validation, or verification:

return redirect()->away('https://www.google.com');

this allows you to redirect to some page outside of your application from the sever-side.

If you change a variable inside the.env file make sure to run php artisan config:cache or php artisan cache:clear so it reads if variables has changed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM