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

I'm not sure but I'm using this and it works well.

$this->app->share(function($app)
{
	return new ViewData;
});
Last updated 2 years ago.
0

App::singleton() for binding a concrete class to the container as its abstract like:

class ConcreteClass implements AbstractInterface {};

And in some constructor:

public function __construct(AbstractInterface $dependency)
{
}

Then if you use:

App::singleton('AbstractInterface', 'ConcreteClass');

Laravel will inject a single instance of the ConcreteClass into the constructor above, this will useful for injecting an adapter. When there are number of adapter implemented same interface, and in your client script just want to 'know' about the interface (abstract), not the specifiec ones (concrete), and using App::singleton() will help you switch to the specific adapter at run-time. This mechanism can be applied to strategy, driver, ... as well.

In your case, using

App::bindShared('viewdata', function(){
    App::make('ViewData');
}); 

is preferred.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Whyounes whyounes Joined 1 Apr 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.