A good folder structure makes a REST API project easier to understand, develop, maintain, and expand. In this lesson, we will create a practical folder structure for our PHP Student Management REST API.
A small project may work with only a few PHP files, but a REST API can quickly grow to contain many endpoints.
A good folder structure helps us:
Our Student Management API can start with the following structure:
student_api/
│
├── config/
├── api/
├── index.php
└── database.php
We will add more folders as the project becomes larger.
The main project folder contains all files related to the REST API.
student_api/
Keeping all API-related files inside one project folder makes the application easier to manage.
The config folder can contain configuration-related files.
student_api/
└── config/
For example, database configuration can be stored here.
We can create a database connection file inside the config folder.
student_api/
└── config/
└── database.php
This file can contain the PDO connection code.
The api folder contains the API endpoint files.
student_api/
└── api/
For example, student-related endpoints can be placed inside this folder.
We can create a file named students.php.
student_api/
└── api/
└── students.php
This file can handle student-related GET, POST, PUT, PATCH, and DELETE requests.
If our application has users, we can create a separate endpoint.
student_api/
└── api/
├── students.php
└── users.php
This keeps user-related operations separate from student operations.
Authentication endpoints can also have separate files.
student_api/
└── api/
├── students.php
├── users.php
└── login.php
Later, this can be expanded to include registration and token-based authentication.
An index.php file can be placed in the project root.
student_api/
└── index.php
It can provide a basic entry point or project information.
student_api/
│
├── config/
│ └── database.php
│
├── api/
│ ├── students.php
│ ├── users.php
│ └── login.php
│
└── index.php
This is a simple starting structure for our REST API project.
Database connection code should not be repeated unnecessarily in every API file.
Instead, create one reusable database connection file.
config/database.php
API files can include this file when a database connection is required.
An API file can include the database connection file.
require_once '../config/database.php';
The exact relative path depends on where the API file is located.
Different resources can have different API endpoint files.
api/
├── students.php
├── courses.php
├── users.php
└── payments.php
This approach keeps resource-specific API logic organized.
The students endpoint can handle operations related to students.
api/students.php
For example:
If the application manages courses, a courses endpoint can be created.
api/courses.php
This endpoint can handle course-related operations.
For larger applications, authentication-related files can be separated into their own folder.
student_api/
└── auth/
For example:
auth/
├── login.php
├── register.php
└── logout.php
student_api/
│
├── config/
│ └── database.php
│
├── api/
│ ├── students.php
│ ├── courses.php
│ └── users.php
│
├── auth/
│ ├── login.php
│ ├── register.php
│ └── logout.php
│
└── index.php
A larger API may benefit from reusable helper functions for JSON responses.
For example:
helpers/
└── response.php
This can help keep response formatting consistent across API endpoints.
Reusable functions can be placed inside a helpers folder.
student_api/
└── helpers/
├── response.php
└── validation.php
This prevents the same utility code from being repeated in multiple files.
Validation functions can be placed in a separate helper file.
helpers/validation.php
For example, validation functions can check required fields or email formats before database operations.
Larger APIs may use versioned endpoints.
api/
└── v1/
└── students.php
A future version could be organized separately:
api/
├── v1/
│ └── students.php
└── v2/
└── students.php
student_api/
│
├── config/
│ └── database.php
│
├── api/
│ └── v1/
│ ├── students.php
│ ├── courses.php
│ └── users.php
│
├── auth/
│ ├── login.php
│ ├── register.php
│ └── logout.php
│
├── helpers/
│ ├── response.php
│ └── validation.php
│
└── index.php
This structure can support a larger REST API project.
Files that perform similar responsibilities should be grouped together.
config/
Database configuration
api/
API endpoints
auth/
Authentication
helpers/
Reusable functions
This makes the project easier to navigate.
Putting the database connection, authentication, validation, and every API operation into one very large PHP file can make the project difficult to maintain.
Separating responsibilities into folders and files makes the application easier to update.
Use names that describe the purpose of each file.
Good examples:
database.php
students.php
login.php
register.php
validation.php
response.php
Clear names help developers understand the project quickly.
The PHP API structure is independent of the React Native project structure. The mobile application communicates with the API through HTTP requests.
React Native Project
↓
HTTP Request
↓
PHP API
↓
MySQL Database
After creating the API folders and files, use Postman to test the endpoint.
GET
http://localhost/student_api/api/students.php
If the endpoint is configured correctly, it should return the expected JSON response.
student_api/
│
├── config/
│ └── database.php
│
├── api/
│ └── v1/
│ ├── students.php
│ ├── courses.php
│ └── users.php
│
├── auth/
│ ├── login.php
│ ├── register.php
│ └── logout.php
│
├── helpers/
│ ├── response.php
│ └── validation.php
│
└── index.php
This structure provides separate locations for configuration, API endpoints, authentication, and reusable helper functions.
A good folder structure makes a REST API easier to build and maintain. Configuration files can be separated from API endpoints, authentication can have its own location, and reusable functions can be stored in helper files. As the project grows, API versions can also be separated.
student_api/
│
├── config/
├── api/
├── auth/
├── helpers/
└── index.php
This structure will be used as the foundation for the upcoming PHP REST API lessons.
Question: Which folder is commonly used to store PHP REST API endpoint files in our project?