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

what about using a model full of static methods and vars, ie

class ObjectModelRepresenation {

	public static $prop1 = null;
	public static $prop2 = null;
	public static $prop3  = null;
	
	public static function init() {
		// initialize your object using the configuarion file
		$file = json_decode( File::get( app_path() . '/assets/config.json' ) );
		static::$prop1 = $file->prop1;
		static::$prop2 = $file->prop2;
		static::$prop3 = $file->prop3;
		// etc
	}
}

because it's a model is automactillay avaiable across all your code

now, you need to initialize before any controllers get trigger, so in filters.php do this

App::before(function($request)
{
	ObjectModelRepresenation::init()
});

now in yours controllers you can access any property by ObjectModelRepresenation::$prop1, so rembember to make all of its internal code public and static

Last updated 2 years ago.
0

Your object:

class Object
{
      public $property1;
      public $property2;

      public function __construct(Array $config)
      {
             $this->property1 = $config['property1'];
             $this->property2 = $config['property2'];
      }
}

In a service provider's register() method:

App::bind('Your\Object', function() {
     return new Your\Object( Config::get('your.config') );
}

Need a shared instance (same object every time)?

App::singleton('Your\Object', function() {
     return new Your\Object( Config::get('your.config') );
}
Last updated 2 years ago.
0

In my opinion @GRAgmLauncher is correct with the suggestion of using App::singleton. Laravel should register the resource as a singleton which is then able to be injected by the IoC via the __construct() method of your controller.

Last updated 2 years ago.
0

Hi, @GRAgmLauncher seems ok to me! Thank you.

After digging into some resources out there, I found that we can of course use Service Providers to manage our bindings.

:)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jrean jrean Joined 4 Mar 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.