0

This is a fairly open question, i'm looking for opinion more than anything...

Coming from Laravel, Dependency Injection (DI) was blissful, if you wanted to access a method from a class, you could either inject it by type hinting in the method or constructor and you'll have instant access to it; symfony2 doesn't seem this simple from the face of it.

We've got service containers, setter injection, constructor injection by setting up a service yml where you pre-define your arguments, but this just seems dirty.

So the "Laravel Way" of dependency injection is done like this

class Listener
{
    protected $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function userWasAdded(User $user)
    {
        // Do some stuff...

        $this->mailer->send('emails.welcome', ['user' => $user], function($message)
        {
            $message->to($user->email, $user->name)->subject('Welcome!');
        });
    }
}

How could I, in Symfony2, replicate Laravel's form of dependency injection, this seems like a whole cleaner way of doing things, or perhaps there is a Symfony2 way of doing things I haven't yet discovered. Any help is appreciated.

5
  • 1
    Examples of working code in Laravel's way would be helpful to understand the problem. I don't think Symfony's way is dirty. YAML configuration is very flexible; Commented Nov 2, 2016 at 9:10
  • 2
    Symfony has an Auto Wiring feature from 2.8, symfony.com/blog/new-in-symfony-2-8-service-auto-wiring. You just need to enable it. Commented Nov 2, 2016 at 9:17
  • @Federkun This is something which is alright on the face of it, but when you get a big project, this could start to become cumbersome.. the YAML file would become a big mess and that's something I want to avoid Commented Nov 2, 2016 at 9:20
  • 1
    @MichaelBrewster you can avoid it by separating config file and import them into services.yml file Commented Nov 2, 2016 at 9:51
  • There is seldom a single "right" way for anything related to development. I find the Symfony service files to be very useful when returning to a large project. The files present a compact overview of the services used and who they interact with. With a magic container I tend to spend considerably more time looking at source code to see how things are wired up. Commented Nov 2, 2016 at 16:47

1 Answer 1

1

I'm not familiar with Laravel but after reading doc I believe that DI in Laravel and Symfony are quite similar after all.

From the doc:

There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.

In this case, Symfony has similar feature starting from 2.8 version which is auto wiring

In other case (meaning that class has constructor dependencies) you need to instruct your framework how to resolve these dependencies. Only difference here is that you have to do it in different ways.

In Symfony most common way is just to use yml file:

services:
    mailer:
        class:     Mailer
        arguments: ['%mailer.transport%']

But you can do it in xml or php as well (check the doc)

$container->setParameter('mailer.transport', 'sendmail');
$container
    ->register('mailer', 'Mailer')
    ->addArgument('%mailer.transport%');

But in Laravel it is the same: you need to instruct framework how to instantiate objects

$this->app->bind('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

Which option to choose to setup container (yml, xml, php) is just a matter of taste.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.