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

I prefer facades

Last updated 2 years ago.
0
Solution

so, i was asking because i often encounter myself doing things like:

IoC::register('controller_name', function()
{
    return new controller_name(IoC::resolve('dependency1'), ...);
});

which is okay when you have just a few controllers, but when you have a lot, this becomes a mess.

so, i was looking for a more automatic replacement, and i found it:

Event::listen(Controller::factory, function($class)
{
	$reflection = new ReflectionClass($class);
	$reflectionParams = $reflection->getMethod('__construct')->getParameters();
	$params = array();
	
	foreach ( $reflectionParams as $param )
	{
		$params[] = IoC::resolve($param->getClass()->name);
	}
	
	return $reflection->newInstanceArgs($params);
});

(laravel 3)

greetings and thanks longilineo for your answer.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ruk33 ruk33 Joined 9 Feb 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.