If you define a service provider and bind services to the container (which is where they are being injected from) you have control of their instantiation.
Thank you trq.
IoC is definitely the way to go and Service Providers might be the right mechanism for what I need.
I think I have a situation that is a bit more complex. Consider a new class called ContextService that provides encapsulation for a set of properties:
<?php
/**
* provides information on the current application context
*/
class ContextService extends BaseService
{
const ENV_WEB = 1;
const ENV_TESTING = 2;
const ENV_COMMAND_LINE = 3;
/** @var int */
private $userId;
/** @var int */
private $accountId;
/** @var string */
private $languageCode;
/** @var int */
private $environment;
/**
* @param int $accountId
*/
public function setAccountId($accountId)
{
$this->accountId = $accountId;
}
/**
* @return int
*/
public function getAccountId()
{
return $this->accountId;
}
/**
* @param int $environment
*/
public function setEnvironment($environment)
{
$this->environment = $environment;
}
/**
* @return int
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* @param string $languageCode
*/
public function setLanguageCode($languageCode)
{
$this->languageCode = $languageCode;
}
/**
* @return string
*/
public function getLanguageCode()
{
return $this->languageCode;
}
/**
* @param int $userId
*/
public function setUserId($userId)
{
$this->userId = $userId;
}
/**
* @return int
*/
public function getUserId()
{
return $this->userId;
}
}
The class should be configured based on the current context. For example:
There can be only one object of type ContextService so I thought I would use:
App::instance('context', $context);
The question is where do I create the $context object? I might need to initialize it in the route before filter and set the $accountId, $languageCode and $environment there. The $userId property might be set once the user is authenticated.
Do I perform the actual object creation in a Service Provider class and if so, will I have access to the request properties (subdomain, parameters, etc...)?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community