Support the ongoing development of Laravel.io →
posted 11 years ago
IOC

Hi all,

I come from a background of developing apps in Symfony2, where instances returned from the service container (the Symfony2 equivalent of the Laravel 4 IoC container) are singletons by default. This is in contrast to Laravel 4, where each request for a class from the IoC container seems to return a new instance where this is not explicitly stated by the presence of either an App::bind or App::singleton call for the class.

Unfortunately, the way in which I build applications requires that the vast majority of instances requested from the IoC container are singletons rather than new instances. With my current understanding, it appears that in order to satisfy this requirement, I not only need to provide an App::singleton call for all singleton classes, but also tell Laravel how to create an instance of them:

App::singleton('App\Logic\AgencyImport\TabularArrayData\TableValidator.unparsedRow', function($app)
{
    return new \App\Logic\AgencyImport\TabularArrayData\TableValidator(
        App::make('App\Logic\AgencyImport\TabularArrayData\CsvBasedBuilder'),
        App::make('App\Logic\AgencyImport\TabularArrayData\UnparsedRowValidator')
    );
});

This is a real shame, because as things stand, the classes within my application have all been automatically bound together by the Automatic Resolution feature of the IoC container and my configuration is therefore very slim as a result. It looks like I'm going to need to create a code block along the above lines for most classes within my application in order to satisfy my need for singleton instances.

Can anybody offer any suggestions to meet my need for singleton instances within having to add large volumes of code to my IoC configuration? Ideally, this would involve changing the default behaviour to create singletons rather than new instances, but I'd also be interested to find out if Laravel offers a way to specify a given class as an IoC singleton without having to tell Laravel how to create that singleton instance.

Thanks,

Jon

Last updated 2 years ago.
0

I'm running into the same problem with my code at the moment.

Did you find a fix to this?

Last updated 2 years ago.
0
  • Firstly, let me guess: Your TableValidator using 2 dependencies is some kind of AbstractBuilder (that the CsvBasedBuilder implements) and AbstractValidator (that the UnparsedRowValidator implements), so the IoC could not resolve the TableValidator ?

  • If I was right, I believe you need some kind of TableValidatorManager that using the AbstractBuilder and AbstractValidator as its driver, you can write your own way of using its driver, such as

    TableValidator::using($builder, $validator);

There's nothing to do with the IoC here. But ofcourse, you can bind the Manager to the IoC after that.

  • And another approach, as I can see you want to make some thing too general, I mean, why don't defined a validator named: App\Logic\AgencyImport\TabularArrayData\TableValidator\UnparsedRow, and use explitcity the UnparsedRowValidator and CsvBasedBuilder (or one of them) just make your validator more specificed, then the automated resolve come back.
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2025 Laravel.io - All rights reserved.