Support the ongoing development of Laravel.io →
posted 10 years ago
Architecture
Last updated 2 years ago.
0

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.

http://laravel.com/docs/ioc#service-providers

Last updated 2 years ago.
0

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:

  • $userId should hold the value of the currently-logged in user
  • $accountId should hold a numeric value that is extracted from the URL in the current request
  • $languageCode should be set based on the subdomain in the current URL request
  • $environment should may have a value based on the context in which the object was set (web access, cron-job, etc...)

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...)?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

nirsharony nirsharony Joined 23 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.