phparchitect-2019-08

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

Symfony 4: A New Way to Develop Applications

Compared to previous versions of Symfony, we have two
new files: .env and symfony.lock. The .env file is used to set
up environment variables, and it can be different for different
environments. You can read more about configuration^17 in
the documentation. Keep in mind .env variables are no more
secure than parameters.yaml; they are only more flexible.
Their main advantages are that they can be changed between
deploys without changing any code and they don’t need to be
checked into the code repository.
One last thing, Flex keeps tracks of the recipes it installed
in a symfony.lock file, which must be committed to your code
repository.


Bundle-Less Application
When I moved from Symfony 1.4 to Symfony 2 bundles
were a strange concept. However, I started using bundles in
each of my projects, and they became natural to me. In the
community, there were many discussions about whether or
not we should use bundles. To be honest, usually, we did not
reuse code from one project to another, so using bundles was
nonsense. Symfony is moving forward to standardization,
and in some situations, it’s much better to provide one way of
handling things than to give too much choice.
So, in Symfony 4, all of your code goes to src/ folder under
the App/ namespace, and support for bundles and inheritance
mechanisms are deprecated and removed. You still organize
your code in any way you want, but you need to do it without
bundles and inside src/ folder. It removes the perceived
complexity and also actually decouples your code from the
Symfony framework.
Still, bundles are not completely out of the game. If you
want to share code between projects, you can still create a
bundle for that, but that part of code should be included via
Composer and should be inside the vendors/ folder. See this
SymfonyCast about bundles^18 for details.

Let’s Add Some Code
Now let’s add some code. Let’s start, as we usually would
when learning something new, with the super exciting “Hello
Wo r l d .”


Go to config/routes.yaml and uncomment the configu-
ration. Reload your local server, and you should receive a
helpful error message, like in Figure 8, that says the controller
is missing. Since we’re in our dev environment, that is done
by default. To turn off error reporting, you need to change
the environment from dev to prod, the same as in previous
versions of Symfony.
You should also check your console, where you see a
message describing what is missing.

17 configuration: https://phpa.me/symfony-config


18 SymfonyCast about bundles: https://phpa.me/symfonycasts-bundles


Let’s add the missing controller, and add the index public
method (see Listing 2). Notice that we don’t require the suffix
action anymore.
Voilà, we have a hello message on our screen, as you can
see in Figure 9.
Okay, since we are doing this very complex and amazing
project that will change the world, we need an excellent
templating engine. Twig is perfect for that, so let’s add it to
our stack. The only thing you need to do is “composer require
template”—you can use Twig or template as an alias, it is the
same thing.

Figure 8

Listing 2


  1. <?php



  2. namespace App\Controller;





  3. use Symfony\Component\HttpFoundation\Response;



  4. class DefaultController

  5. {

  6. public function index()

  7. {

  8. return new Response('Hello');

  9. }

  10. }


Figure 9
Free download pdf