The story
Let's take this request https://nike.awesomedomainname.com/
as an example. I need a service that can fetch the nike
.
What I currently have
I've a service that fetches a router key from the @request_stack
. Let's use company as router key.
My main route as followed;
<import host="{company}.domain.{_tld}" prefix="/" schemes="https" resource="@ExampleBundle/Resources/config/routing.xml">
<default key="_tld">%app_tld%</default>
<requirement key="_tld">com|dev</requirement>
<requirement key="company">[a-z0-9]+</requirement>
</import>
So I wrote this service to fetch the key and search for the company;
...
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class CompanyContextRequest {
protected $request;
protected $companyRepository;
public function __construct(RequestStack $requestStack, CompanyRepository $companyRepository) {
$this->request = $requestStack->getMasterRequest();
$this->companyRepository = $companyRepository;
}
public function getCompany()
{
if (null !== $this->request)
{
$company = $this->request->attributes->get('company');
if (null !== $company)
{
return $this->companyRepository->findOneBy([
'key' => $company
]);
}
}
}
....
}
And the service xml definition;
<service id="app.company_context_request" class="AppBundle\Request\CompanyContextRequest">
<argument type="service" id="request_stack"/>
<argument type="service" id="orm_entity.company_repository"/>
</service>
<service id="app.current_company" class="AppBundle\Entity\Company">
<factory service="app.company_context_request" method="getCompany"/>
</service>
Now my problem is that in some cases the service app.current_company
doesn't return a Company
object instead a null
is given.
For example I have a custom UserProvider
that has the CompanyContextRequest
as a dependency to check if the user belongs to the company but I can't do so because it returns null
.
But it works perfectly in controllers and most other DI places.
Any suggestions? Is there something like priority for services like for event subscribers?
What I've tried
I've set the scope app.company_context_request
into request
and tried to fetch it from the container. With false results.
According to the Symfony docs this should work.
I run the latest stable version of Symfony v2.7.3
Edit: My goal is to have a service at all times that can tell me the current company that is based ofcourse on the subdomain.
Edit 2: This example is almost what I need except that in this example they use the users company and I need the company(key) from the subdomain.