New in Symfony 4.2: Important deprecations
Symfony's Backward Compatibility Promise ensures smooth upgrades in your projects because it forbids backward compatibility breaks in minor releases. Instead of changing or removing existing features, we mark them as deprecated and change them in the next major Symfony version.
The UPGRADE-4.2.md document explains all deprecations in details and you'll see them too in the Web Debug Toolbar, the profiler and when running tests. This article summarizes the most important deprecations so you can start upgrading your Symfony 4 apps.
Deprecated a template directory¶
Storing the app templates in
src/Resources/views/ is now deprecated. You
must store them in the directory defined in the
config/packages/twig.yaml, which is
templates/ by default.
Deprecated the Kernel name and the root dir¶
KernelInterface::getName() method and the
kernel.name parameter have
been deprecated. There's no alternative to them because this is a concept that
no longer makes sense in Symfony applications.
If you need a distinctive ID for the kernel of the application, you can use the
KernelInterface::getContainerClass() method and the
getRootDir() method and the
have been deprecated too. The alternative is to use the
kernel.project_dir method introduced in Symfony 3.3:
1 2 3 4 5 6 7 8 9
# 'root_dir' is where the Kernel class is stored (src/ by default) and # 'project_dir' is the main project directory services: _defaults: bind: # Before $dataDir: '%kernel.root_dir%/../var/data/' # After $dataDir: '%kernel.project_dir%/var/data/'
Deprecated some console options¶
We've removed the original content of this blog post section because the related change was reverted before the Symfony 4.2 release. You can keep using both the console options and the environment variables:
1 2 3 4 5
# Using options to control the environment and the debug behavior $ php bin/console command_name --env=test --no-debug # Using env vars to control the environment and the debug behavior $ APP_ENV=test APP_DEBUG=0 php bin/console command_name
ContainerAwareCommand class has been deprecated. It was used in the past
to create commands extending from it so they had direct access to the app
service container. The alternative is to extend commands from the
class and use proper service injection in the command constructor, as explained
in the main article about the Symfony Console.
TIP: use the
make:command utility provided by the MakerBundle to
generate commands quickly and following the Symfony recommendations.
Deprecated the base
Controller class is the optional class your controllers can extend
from to get access to some useful shortcut methods (such us
Controller class has been deprecated in favor of
AbstractController base class has the same shortcuts but it's more
restrictive about services. You cannot use the
$this->get() shortcut to get
services. You must follow the modern practices of injecting services in your
controller constructor or in the controller action.
TIP: use the
make:controller utility provided by the MakerBundle to
generate controllers quickly and following the Symfony recommendations.
Deprecated process commands as strings¶
Passing commands as strings to the
Process class has been deprecated. The
alternative is to pass an array of the command parts (name, arguments, options):
1 2 3 4 5 6 7
use Symfony\Component\Process\Process; // Before $process = new Process('ls -l'); // After $process = new Process(['ls', '-l']);
Deprecated tree builders without root nodes¶
This deprecation won't affect directly to most developers because it's related to the configuration classes of dependency injection. However, you'll see lots of these deprecation messages because of the bundles used in your application.
Fixing this deprecation will be simple in most cases, so you may contribute a fix to your favorite third-party bundle:
1 2 3 4 5 6 7 8 9 10
use Symfony\Component\Config\Definition\Builder\TreeBuilder; // Before $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('acme_root'); $rootNode->...()->...()->...(); // After $treeBuilder = new TreeBuilder('acme_root'); $treeBuilder->getRootNode()->...()->...()->...();
New in Symfony 4.2: Important deprecations symfony.com/index.php/blog/new-in-symfony-4-2-important-deprecationsTweet this
Become a certified developer! Exams are online and available in all countries.Register Now
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.