Organizing My Symfony Projects
Symfony is the first and only web framework I have ever used. It’s an MVC abstraction of the PHP programming language that allows developers to boost their development speed while keeping application performances high. My tech stack is a bit more complicated than that however, so I find it interesting to dive into it.
Symfony is a flexible full-stack framework, so you can work on every part of your application. My project repository is organized as follow:
/assets
/css
/fonts
/icons
/js
/api
/components
/pages
/shell
/blog
/public
/build
/img
/upload
/config
/src
/Controller
/API
/Cron
/Webhook
/DataFixtures
/Entity
/Security
/Service
/static
/templates
/tests
.env
webpack.config.js
tailwind.config.js
I let PHP handle most back-end tasks in the /src folder.
Doctrine is used as an Object Relational Mapper to manage the data access layer and convert database entities into PHP classes (/src/Entity).
The Model is located in different repositories: /src/Service for the services used to manage the application (CRUD and others) and /src/Security to take care of authentication and authorization.
The Controller is in the /src/Controller repository. I add my application routes here. Most of them return dynamic HTML pages, but I put there API endpoints, Cronjobs, and webhooks.
Most of the back-end configuration (database accesses, API keys, etc.) is done using a root .env file and YAML files located in the /config directory.
Of course, unit testing is not neglected, which is why there are a /test and a /src/DataFixtures directories. PHPUnit is used to run tests, and Doctrine takes care of inserting fixtures.
The View is handled differently depending on which web development philosophy I choose. /templates contains Twig templates that are consumed by the back-end to create rich webpages or assets. When I use a static approach, all the files are written to the /static directory. Otherwise, they are directly generated and sent to the user at run time.
I use a combination of React, TailwindCSS, and SASS as a front-end framework. All my assets are located in the /assets directory and dynamically built by Webpack Encore (Symfony’s Webpack flavor).
My Javascript source folder is divided between API functions, generic React components used throughout the application, and page-specific React components. I’m also adopting an offline-first approach, hence the /shell directory.