loading...

Symfony, the "not magic" parts

jmau111 profile image Julien Maury Updated on ・2 min read

Symfony is not just another PHP framework. It's a fantastic set of decoupled and reusable PHP components that allow building easy to maintain and large scale projects.

It relies on one of the most powerful dependency managers out there, a great tool named composer.

Just run :

composer create-project symfony/skeleton myAwesomeProject

and you're good to go.

Dependency injection

Believe it or not, it's pretty simple. Look at the following code:

public function myMethod($args) {
    // some code before instanciation
    // ...
    $object = new MyAwesomeClass($args);
    // some code after instanciation
    // ....
    return $object->myAwesomeMethod();
}

this is everything but dependency injection. The object is directly instantiated inside the method.

Is it bad?

Well, kinda... the method depends on both the MyAwesomeClass object and the value used to instantiate it.

Instead, it's better to have structures described in this link.

The object is directly injected with a type-hint. It's great to decouple your code. Besides, it is easier to test.

Autowiring

Symfony brings an extra cool way to automate dependency injections. Just make sure you have the following in a .yaml file located in config/services.yaml:

services:
    _defaults:
        autowire: true
        autoconfigure: true

and now you can easily call any service or interface in your code with a simple type-hint:

namespace App\Controller;

use App\Service\MyAwesomeService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class DefaultController extends AbstractController
{
    public function index(MyAwesomeService $awesome)
    {
       print_r($awesome);
    }
}

Source

You make clean injections with minimal configurations.

Autoconfiguring

As the name suggests, the idea is to automatically apply some configurations to your services. It's often combined with the autowire option:

services:
    _defaults:
        autowire: true
        autoconfigure: true

Symfony allows using tags. It's a mechanism that flags services that require special processing (compilation) such as Twig extensions.

Without the autoconfigure option it would be much more difficult and time-consuming to tag your services. You would have to list them manually but this would make the autowiring mechanism kinda useless...

Automatic, not magic

The purpose of autowiring and autoconfiguring is to automatize the configuration. The idea is to write shorter configuration files.

You don't have to use these options unless you need some automation.

Posted on by:

jmau111 profile

Julien Maury

@jmau111

Practise what you preach.

Discussion

pic
Editor guide
 

Yes, exactly! Symfony is awesome and even for small scale projects, I prefer Symfony.

 
Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

Your DI example is bad. Really bad. No point in reading any further.

 

I find your comment quite inappropriate. But still, nobody's perfect so, in doubt, I've updated with a link. Hope it's less confusing...