phparchitect-2019-08

(Rick Simeone) #1
http://www.phparch.com \ August 2019 \ 9

Symfony 4: A New Way to Develop Applications

As you can see in Figure 10, with
this one single command, Symfony
flex installs and fully configures Twig
templating. If we want to remove it the
only thing we need is to write in console
composer remove twig and all files and
configuration are removed automati-
cally. Awesome, right?
In Listing 3, I rewrote the controller
a little bit as I wanted to get access to
helper methods. Please notice I extend
AbstractController, not the Controller
class. Controller class is deprecated. The
main thing here is that you don’t have
any more direct access to $container, as
that is useless, so if you try $contain-
er->get('my_service') it won’t work.
Be sure to check the AbstractController
class to see what it does.
I am sure you’ll want to have profiler while you are devel-
oping your apps—just run composer require profiler. It is
automatically added and configured for your test and devel-
opment environment.
Our Hello World controller looks like Listing 4.
With routing:

index:
path: /{name}
controller: App\Controller\DefaultController::index

And template:

{% extends 'base.html.twig' %}

{% block body %}Hello {{ name }}{% endblock %}

Symfony 4 automates configuration so you can keep coding
features and reduce the time that you are spending inside
YAML files configuring your application. This is achieved
with providing the great error messages that we already saw,
but the main thing here is autowiring.


Autowiring And Configuration
Although Symfony 3 introduced Autowiring^19 , it is not some
new Symfony 4 thing. But, in combination with Symfony Flex,
it got better.
Autowiring allows you to manage services in the container
with minimal configuration. It reads the type hints on your
constructor (or other methods) and automatically passes
the correct services to each method. Symfony’s autowiring is
designed to be predictable: if it is not absolutely clear which
dependency should be passed, you’ll see an actionable excep-
tion.

19 Autowiring: https://phpa.me/symfony-autowiring


Some developers don’t use it because of all the magic that is
happening during autowiring. However, just like most impres-
sive magic tricks, there is no magic at all, and it is quite simple.
In the past, we needed to define everything manually when
we were creating services. First, we needed to create a class,
and then we defined that class as service in our configuration
file (services.yml, for example). All services have a unique ID
(name) and class that matches that service, previously defined.

Figure 10

Listing 3


  1. <?php



  2. namespace App\Controller;



  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;



  4. class DefaultController extends AbstractController

  5. {

  6. public function index()

  7. {

  8. return $this->render('index.html.twig');

  9. }

  10. }


Listing 4


  1. <?php



  2. namespace App\Controller;



  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;



  4. class DefaultController extends AbstractController

  5. {

  6. public function index($name)

  7. {

  8. return $this->render('index.html.twig', [

  9. 'name' => $name

  10. ]);

  11. }

  12. }

Free download pdf