Open DeveloperOnCall opened 5 years ago
DailyEmail most likely needs to implement the TenantAwareJob trait, see: https://laravel-tenancy.com/docs/hyn/5.3/queues
I tried a few things - including your recommendation. Not luck yet.
Curious - I replaced the method that I am using with the following - just to see if the url('/') is working properly up to that point.
$repository = app(WebsiteRepository::class);
$websites = $repository->all();
//dd( $websites ); // THIS WORKS I am given the first web site details
foreach( $websites as $siter ) {
$environment = \App::make(\Hyn\Tenancy\Environment::class);
$environment->tenant($siter);
dd( url('/'), 'There');
}
Which very strangely returns:
"http://localhost"
"There"
From what I understand I should be getting the URL to the current (first iteration) web site's url.
What am I doing wrong here?
Thanks, Daniel
Humor me and try using config('app.url')
instead of url
Hi @bkintanar thanks for chiming in here..
I gave that a try and am returned the same 'localhost' value.
D
Ok. what does your TENANCY_AUTO_HOSTNAME_IDENTIFICATION
in your .env
looks like?
What makes it a little more confusing is that we are using the tenant.database at that point - when I dump a value from the db - it is using the first web sites database properly.
Problem just seems to be the URL.
To your other question - I do not have that set in .env rather setting the default in the config/tenancy.php
'auto-identification' => env('TENANCY_AUTO_HOSTNAME_IDENTIFICATION', true),
D
Well the thing is that the setting of the update-app-url
happens in the HostnameActions
middleware, and only if auto-identification is enabled. Which you claim it's set to true.
For argument's sake I set it in .env here are the related entries
TENANCY_DATABASE_AUTO_DELETE=true
TENANCY_DATABASE_AUTO_DELETE_USER=true
TENANCY_DEFAULT_CONNECTION=system
AUTO_DELETE_TENANT_DIRECTORY=true
TENANCY_EARLY_IDENTIFICATION=true
TENANCY_AUTO_HOSTNAME_IDENTIFICATION=true
Still returning localhost...
Looks like this is a bug then. HostnameActions is a middleware so it's only executed when an http request is made.
@luceos ?
Could it be the loop? I hacked the src (recommended on an issue from some time ago - to be able to loop through all sites (in order to run a job on each site)
$repository = app(WebsiteRepository::class);
$websites = $repository->all();
Which does work to loop all sites. Then using the following to set the environment etc
foreach( $websites as $siter ) {
$environment = \App::make(\Hyn\Tenancy\Environment::class);
$environment->tenant($siter);
// This works to give me the database (and Storage I believe)
}
I don't believe it's the loop since you're able to get the correct hostname out of it. I believe it's (app.url) not being set at all.
Just to further help any debug here -- I made this change
$repository = app(WebsiteRepository::class);
$websites = $repository->all();
//dd( $websites );
foreach( $websites as $siter ) {
$environment = \App::make(\Hyn\Tenancy\Environment::class);
$environment->tenant($siter);
// Get current Hostname
$hostname = app(\Hyn\Tenancy\Environment::class)->hostname();
// Get FQDN (Fully-Qualified Domain Name) by current hostname
$fqdn = $hostname->fqdn;
dd( config('app.url'), $fqdn);
}
Strangely I am not getting the FQDN either. This is what is dumped
"http://localhost"
null
You cant get the hostname that way.
$env = app(Environment::class);
$env->tenant($tenant);
$hostname = $env->hostname();
Can you try this code above?
No change..
$repository = app(WebsiteRepository::class);
$websites = $repository->all();
//dd( $websites );
foreach( $websites as $siter ) {
//$environment = \App::make(\Hyn\Tenancy\Environment::class);
//$environment->tenant($siter);
$env = \App(\Hyn\Tenancy\Environment::class);
$env->tenant($siter);
$hostname = $env->hostname();
// Get current Hostname
dd( $hostname, 'bkintanar Host' );
// Get FQDN (Fully-Qualified Domain Name) by current hostname
$fqdn = $hostname->fqdn;
dd( config('app.url'), $fqdn);
}
Here is the dump
null
"bkintanar Host"
When I dd($siter) in the above loop...
Hyn\Tenancy\Models\Website {#8910
#connection: "system"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [
"id" => 22
"uuid" => "e2b798f1a01e43f1832d3bfb051298aa"
"customer_id" => 22
"created_at" => "2018-05-29 16:55:19"
"updated_at" => "2018-05-29 16:55:19"
"deleted_at" => null
"managed_by_database_connection" => null
]
#original: array:7 [
"id" => 22
"uuid" => "e2b798f1a01e43f1832d3bfb051298aa"
"customer_id" => 22
"created_at" => "2018-05-29 16:55:19"
"updated_at" => "2018-05-29 16:55:19"
"deleted_at" => null
"managed_by_database_connection" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#forceDeleting: false
}
Is this normal?
D
For clarity sake - I only made the one change to the multi-tenant core.. (Hyn\Tenancy\Repositories\WebsiteRepository)
// Hack to enable looping through all websites (used to perform a job on all sites)
public function all() {
return $this->website->all();
}
I'm using version 5.1 in my project and I have the same problem. url('/')
returns always the value stored inside the .env
file under the APP_URL
key, even though I'm successfully switching tenants.
I finally managed to solve it by forcing Laravel's UrlGenerator
class to use a custom root url:
app(UrlGenerator::class)->forceRootUrl(request()->getScheme() . '://' . $hostname->fqdn);
@mauricius Thanks -- but I am not even able to access the $hostname - digging into that at the moment.
D
@mauricius interesting method call, we might be able to use that in the package as well.. Feel free to PR that if you like.
@luceos you’re right the comment on the other issue is related. Is this a bug or still a support problem or something I’m doing wrong?
Curious: what you get on command line interface when trying:
$ php artisan tenancy:run --tenant=1 route:list
I am having also problems with generating urls. Indeed, with 'URL::forceRootUrl()` the url can be set on command line interface by first finding the first() hostname of the tenant.
But generating an url for a named route doesn't work on cli, in a Command e.g.:
echo route('my.custom-route');
In a Controller the code does work perfectly!
Could you tell me if you can list your tenant routes on the command line?
Sorry, maybe I hijacked your issue a little with my question... That wasn't my intention. I will open a new issue myself.
+1
Description
When running an artisan job that loops through all web sites - the URL within the results (an email) is set to localhost. I do have the 'update-app-url' set to true.
It is using a Laravel Mailer to generate an email and then 'jdavidbakr/mail-tracker' (2.1) to replace links and add a tracking pixel to the email.
Actual behavior
Sets the tracking URL to http://localhost/ throughout the entire email as well as the tracking pixel and 'view' event on that email never fires as a result
Expected behavior
Should use the app.url to the current active tenant
Information
tenancy.php config
webserver.php config
Error log