symfony - How can i better understand service layer in symfony2 in php -


I'm learning to use symfony2, but I can get used to the service level and where it is difficult to learn The time is coming.

I would try my best to explain services, but Symphony 2 does a better job, What i can do

In its original state, one service is only one class. A class becomes a service when it is registered for Symfony2's dependency injection container (or simply container .) At this point, Is part of the service layer.

There may be a service dependency (and many times do this), but it is not necessary. One service is often used as a dependency for other services. I give you an example:

  • You have a class to send email to, which is EmailSender
  • is a class in which the instructions are smtptransport
  • emailcorder as smtpTransport A dependency is called. In other words, in order to be able to do its job for emailcorder , it needs to be injected with SmtpTransport (note that though emailcorder Depending on it, SMTPTransport .)

    Say you have EmailSender in every app in five different places in your app, You may have code like this:

      $ emailSender = new emailender (new SMTPTransport ()); $ EmailSender- & gt; Send ($ email)   

    This is not a lot of code (since emailcorder is only a dependency), but what happens when you make this decision SMTP Instead of using, should use a transport class for emailcorder to sendmail ? You must edit each block of code to update it. The

    option is to register emailcorder and its dependencies as services. Registration looks like this:

      // Your App / Your Bundle / Resources / Config / Services Services: smtp_transport: class: YourApp \ YourBundle \ Email \ SmtpTransport Email_sendress: Class: YourApp \ YourBundle \ Email \ EmailSender Logic: @smtp_transport   

    Now, to use it ( container ):

      Get $ container-> ('EMAIL_SENDER') - & gt; Send ($ email);   

    Note how easy and clean the class is to actually send email? Of course, you have to work to register the service first, but it is trivial and must be done only once.

    Imagine that there are three dependencies instead of one in the EmailSendor , using the services, you will not be able to change service code without changing your implementation code (the code used to send email) Need to modify.

    In short, the service level serves as the central repository for classrooms. A) One or more dependencies are required. Use of a code in the 'global' field is required. Dependency Injection Container , you create an easy way to make less redundant codes and changes with wide reach easier.

    I may be wrong, but I think Symfony2 is the first PHP framework to implement a dependency injection container, as many PHP devs can be unfamiliar with the concept of services. I definitely want to make sure that you understand it completely, because you will see that this is a very powerful tool, and the concept of limited services limited to symphony 2 or even php is not limited. - You will be able to take advantage of this concept in wide cross-points and languages.

Comments