Since Laravel 5.4.16 the $container->makeWith() method allows functionality similar to old $container->make(class, params) functionality, see https://github.com/laravel/framework/pull/18271 and https://github.com/laravel/framework/pull/18320.
So you can do:
$container = new Container;
$value = $container->makeWith(ContainerMixedPrimitiveStub::class, ['first' => 'taylor', 'last' => 'otwell']);
But you can't do:
$container = new Container;
$value = $container->makeWith(ContainerMixedPrimitiveStub::class, [0 => 'taylor', 2 => 'otwell']);
I've backported the ContainerTest::testPassingSomePrimitiveParameters() of Laravel 5.3 for Laravel 5.4:
public function testPassingSomePrimitiveParameters()
{
$container = new Container;
$value = $container->makeWith(ContainerMixedPrimitiveStub::class, ['first' => 'taylor', 'last' => 'otwell']);
$this->assertInstanceOf(ContainerMixedPrimitiveStub::class, $value);
$this->assertEquals('taylor', $value->first);
$this->assertEquals('otwell', $value->last);
$this->assertInstanceOf(ContainerConcreteStub::class, $value->stub);
$container = new Container;
$value = $container->makeWith(ContainerMixedPrimitiveStub::class, [0 => 'taylor', 2 => 'otwell']);
$this->assertInstanceOf(ContainerMixedPrimitiveStub::class, $value);
$this->assertEquals('taylor', $value->first);
$this->assertEquals('otwell', $value->last);
$this->assertInstanceOf(ContainerConcreteStub::class, $value->stub);
}
And it failed at $value = $container->makeWith(ContainerMixedPrimitiveStub::class, [0 => 'taylor', 2 => 'otwell']); line :
1) Illuminate\Tests\Container\ContainerTest::testPassingSomePrimitiveParameters
Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #0 [ <required> $first ]] in class Illuminate\Tests\Container\ContainerMixedPrimitiveStub
So, is it possible to backport this functionality, please?
This is intentional, you have to pass the parameter name.
I would love to be able to understand what was removed and then brought back since this subject seems to be widely discussed around here.
Given:
class SomeClass
{
public function __construct(AnotherResolvableClass $class, $first, $last){}
}
You can use the container to create an instance of this class like so:
$container->makeWith(SomeClass::class, ['last' => 'lastname', 'first' => 'firstname']);
The container resolves the primitive dependencies of the constructor using the parameters passed to makeWith() as second parameter.
This functionality used to be in make() but was removed in 5.4 and then was brought back as makeWith() with a minor difference which is you have to pass an associative with keys as the dependency name.
Most helpful comment
Given:
You can use the container to create an instance of this class like so:
The container resolves the primitive dependencies of the constructor using the parameters passed to
makeWith()as second parameter.This functionality used to be in
make()but was removed in 5.4 and then was brought back asmakeWith()with a minor difference which is you have to pass an associative with keys as the dependency name.