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
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') );
}
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.
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.
:)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community