Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pilat's avatar
Level 41

Is there a way to access class from a service provider

Hi, here's what on my mind:

<?php

// app/Http/Commands/TestCommand.php:
class TestCommand extends Command
{
    public string $subdomain = 'foobar';

    public function handle(Tenant $tenant)
    {
        $this->info("{$tenant->subdomain()} selected");
    }
}

// app/Providers/TenantProvider.php
class TenantProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind(Tenant::class, function (Application $app, $parameters) {

			if ($app->runningInConsole()) {

				$subdomain = $app->***theClassIAmRequestedIn***->subdomain
					?? theClassIAmRequestedIn->subdomain->getArguments()[0]
					?? null;

				return Tenant::for($subdomain);
			}
        });
    }
}

I know I can access request() if it's a web-request, but Commands, Actions, Jobs -- is there a way to get some context about "what's executed right now" when resolving something from a service container?

0 likes
2 replies
Snapey's avatar

Not sure what context you are expecting? A command boots the framework and runs completely isolated from anything else.

1 like
pilat's avatar
Level 41

@Snapey I'd like to know (inside the service provider) what class has initiated this work. Was is a job or a command? What job? What command? Ideally, to read that command/job's properties, but this can be too much to ask ))

Please or to participate in this conversation.