PHP Project Structure
A scalable and maintainable folder structure for PHP applications
File Structure
- src
- App
- Controllers
- HomeController.php
- UserController.php
- ApiController.php
- Models
- User.php
- Post.php
- BaseModel.php
- Services
- UserService.php
- EmailService.php
- Middleware
- AuthMiddleware.php
- CorsMiddleware.php
- Core
- Router.php
- Database.php
- Application.php
- Config
- database.php
- app.php
- resources
- views
- home.php
- layouts
- partials
- assets
- css
- js
- images
- public
- index.php
- assets
- tests
- Unit
- Feature
- TestCase.php
- database
- migrations
- seeds
- vendor
- composer.json
- phpunit.xml
- .env.example
- .htaccess
- .gitignore
- README.md
Directory Structure Explanation
src/App/
Main application namespace containing controllers, models, and services organized by functionality.
App/Services/
Service classes that encapsulate business logic separate from controllers.
App/Middleware/
Middleware classes for processing requests before they reach controllers.
src/Core/
Core framework components like routing, database, and application bootstrap.
Core/Router.php
Routing system that maps URLs to controller actions.
src/Config/
Configuration files organized by concern (database, application, etc.).
resources/
Contains views and raw assets that are processed during build.
resources/views/partials/
Reusable view components that can be included in multiple templates.
tests/
Contains unit and feature tests organized by test type.
database/
Database-related files including migrations and seed data.
phpunit.xml
PHPUnit configuration for running tests and code coverage.