Lesson 47 of 158 – PHP API Folder Structure
47%

PHP API Folder Structure

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.

Note: A well-organized project separates configuration, database connections, API endpoints, and other responsibilities into appropriate folders and files.

1. Why Folder Structure is Important

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:

  • Find files easily
  • Separate responsibilities
  • Maintain the project
  • Add new APIs
  • Debug problems
  • Work with other developers

2. Basic Project Structure

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.

3. Main Project Folder

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.

4. Config Folder

The config folder can contain configuration-related files.

student_api/
└── config/

For example, database configuration can be stored here.

5. Database Configuration File

We can create a database connection file inside the config folder.

student_api/
└── config/
    └── database.php

This file can contain the PDO connection code.

6. API Folder

The api folder contains the API endpoint files.

student_api/
└── api/

For example, student-related endpoints can be placed inside this folder.

7. Students API File

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.

8. Users API File

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.

9. Authentication API File

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.

10. Index File

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.

11. Complete Basic Structure

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.

12. Separating Database Code

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.

13. Including the Database File

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.

14. API Endpoint Separation

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.

15. Students Endpoint

The students endpoint can handle operations related to students.

api/students.php

For example:

  • Get all students
  • Get one student
  • Add student
  • Update student
  • Delete student

16. Courses Endpoint

If the application manages courses, a courses endpoint can be created.

api/courses.php

This endpoint can handle course-related operations.

17. Authentication Folder

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

18. Expanded Project Structure

student_api/
│
├── config/
│   └── database.php
│
├── api/
│   ├── students.php
│   ├── courses.php
│   └── users.php
│
├── auth/
│   ├── login.php
│   ├── register.php
│   └── logout.php
│
└── index.php

19. Response Helper

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.

20. Helpers Folder

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.

21. Validation File

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.

22. API Version Folder

Larger APIs may use versioned endpoints.

api/
└── v1/
    └── students.php

A future version could be organized separately:

api/
├── v1/
│   └── students.php
└── v2/
    └── students.php

23. Complete Expanded Structure

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.

24. Keep Related Files Together

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.

25. Avoid One Large PHP File

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.

26. Naming Files Clearly

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.

27. Folder Structure and React Native

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

28. Test the Structure with Postman

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.

29. Recommended Project Structure

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.

30. PHP API Folder Structure Summary

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.

📌 Key Points

  • A good folder structure makes an API easier to maintain.
  • The main project folder contains the REST API project.
  • The config folder can contain database configuration.
  • The api folder contains API endpoint files.
  • Authentication files can be separated into an auth folder.
  • Reusable functions can be stored in a helpers folder.
  • API resources can have separate endpoint files.
  • API versions can be separated using version folders.
  • Clear file names make projects easier to understand.
  • Database connection code should be reusable instead of duplicated.
  • Postman can be used to test endpoints after creating the structure.
  • A well-organized API is easier to connect with a React Native application.

🧠 Quick Quiz

Question: Which folder is commonly used to store PHP REST API endpoint files in our project?