How to change namespace of Symfony 4 Project

These steps are tested on Symfony 4.2 to change the namespace from “App” to your desired one. The updated namespace will work not only in production and development but also in testing.

There are two approaches to change namespace in Symfony project. One, when you want to complete replace the “App” namespace with your desired one. It can cause you a problem while using Symfony Flex.  To do this, you will have make changes to following files.

  • config/routes.yml [FOR EVERY ROUTE]
  • composer.json [BOTH FOR autoload AND autoload-dev]
    Don’t forget to perform composer dump-autoload after making the change.
  • services.yml [UPDATE THE DEFAULTS OF SKELETON TO LOAD SERVICES]
  • src/Kernel.php
  • public/index.php
  • .env.test [PHPUNIT needs it]

Secondly, you can update the namespace of your custom files, while keeping framework files and its namespace untouched. In this method, you will be using two namespaces pointing to same folder “src/”

  • config/routes.yml [FOR EVERY ROUTE]
  • composer.json [BOTH FOR autoload AND autoload-dev]
    Don’t forget to perform composer dump-autoload after making the change.
    Keep “App” namespace in autoload with your desired one, while in autoload-dev you will only need one desired namespace.
  • services.yml [UPDATE THE DEFAULTS OF SKELETON TO LOAD SERVICES]

In both approaches, you will have to update the namespace of all controllers, services, entities, repositories, and tests if you have already created any.

Leave a Reply

avatar
  Subscribe  
Notify of