Welcome to the blog of Mr.G
When you're working on a PHP-focused project, the structure can differ from a typical front-end-focused project (like one with JavaScript, SCSS, and HTML). PHP-based projects are often server-side applications, and their folder structure depends on the frameworks, deployment strategy, and how the application is built.
Here's a breakdown of how a PHP-based project differs and a recommended structure:
PHP is Server-Side:
src and dist, as PHP files are not "built" or "compiled"; they are interpreted directly by the server.Focus on Backend Logic:
Deployment is Different:
Here’s a standard folder structure for a PHP project:
project
├── public // Publicly accessible files
│ ├── index.php // Entry point (e.g., front controller)
│ ├── css
│ │ └── styles.css // CSS files
│ ├── js
│ │ └── script.js // JavaScript files
│ ├── images
│ │ └── logo.png // Images accessible by the browser
│ └── uploads // User-uploaded files (if applicable)
├── src // Application source code (non-public)
│ ├── controllers // PHP controllers
│ │ └── HomeController.php
│ ├── models // Database models
│ │ └── UserModel.php
│ ├── views // HTML templates or views
│ │ └── home.php
│ └── helpers // Utility/helper PHP functions
│ └── Utils.php
├── config // Configuration files
│ ├── database.php // Database connection settings
│ └── app.php // Application settings
├── vendor // Composer dependencies (if using Composer)
├── storage // Temporary files (e.g., logs, cache)
│ ├── cache
│ ├── logs
│ └── sessions
├── tests // Test scripts (optional)
│ ├── Unit
│ └── Integration
├── .env // Environment variables (e.g., database credentials)
├── composer.json // Composer dependency configuration
├── README.md // Documentation
└── .htaccess // Apache configuration (if applicable)
public/index.php: The entry point for the application. This is often referred to as the "front controller" in frameworks like Laravel.src/controllers/: Handles incoming requests and prepares data for views.models/: Manages the data and database interactions.views/: Contains HTML templates mixed with PHP logic (e.g., home.php).helpers/: Stores reusable utility functions (e.g., date formatters, string helpers).config/.env file or similar.vendor/storage/tests/.env: Stores sensitive information (e.g., database credentials, API keys). This file should never be committed to version control.composer.json: Defines your dependencies if you're using Composer..htaccess: For Apache-specific rules (e.g., redirecting all requests to index.php).In PHP projects, static files like CSS and JavaScript are still stored in the public folder but are kept separate from the PHP code.
Example of Static Files in public/:
public
├── css
│ └── styles.css
├── js
│ └── script.js
├── images
│ └── logo.png
Why Keep Static Files Separate?
public ensures clarity and security. PHP files (e.g., src/controllers) are not directly accessible by the browser.| Aspect | PHP Project | Front-End Project |
|---|---|---|
| Build Process | Typically no build process; PHP is interpreted directly | Often involves bundling, transpiling, and minification |
| Static Assets | Stored in public/ |
Often generated in dist/ |
| Code Organization | Backend logic in src/ |
Code split into src/ and dist/ |
| Deployment | Entire project may be deployed (except src/ subfolders) |
Only dist/ folder is deployed |
Development:
src/.public/.Production:
src/ using .htaccess or server configuration.public/ is exposed to the web.It's better not to let users directly call or access files inside the /src folder. Here’s why:
/srcSecurity Risks:
/src often contain sensitive logic, database queries, or configuration details. If exposed, this could lead to vulnerabilities like SQL injection, data exposure, or unauthorized access to your system.Example:
If a user can access src/helpers/Utils.php directly via http://example.com/src/helpers/Utils.php, they might:
Separation of Concerns:
/src is meant for your application's internal logic. It should not be exposed to the browser.public/index.php), which calls files in /src as needed.Flexibility in Development:
/src directly, changing the folder structure or file names could break your application.index.phpThe front controller pattern is a common best practice in PHP applications. It routes all user requests through a single file, typically index.php in the public/ folder. This file determines how to process the request and includes the appropriate PHP files from /src.
User Requests:
http://example.com/home.public/index.php.Application Logic in index.php:
index.php determines which controller or logic in /src should handle the request.Controller Handles Request:
HomeController in /src/controllers processes the request and returns an HTML response to the user.project
├── public
│ ├── index.php // All requests go through here
│ ├── css
│ │ └── styles.css // Public static files
│ ├── js
│ │ └── script.js
│ └── images
│ └── logo.png
├── src
│ ├── controllers
│ │ └── HomeController.php
│ ├── models
│ │ └── UserModel.php
│ ├── views
│ │ └── home.php
│ ├── helpers
│ │ └── Utils.php
│ └── config.php
public/index.php<?php
// Load application configuration
require_once __DIR__ . '/../src/config.php';
// Autoload classes (if using Composer or custom autoloader)
require_once __DIR__ . '/../vendor/autoload.php';
// Parse the requested URL
$requestUri = $_SERVER['REQUEST_URI'];
// Simple routing logic
switch ($requestUri) {
case '/':
case '/home':
require_once __DIR__ . '/../src/controllers/HomeController.php';
$controller = new HomeController();
$controller->index(); // Call a method in HomeController
break;
case '/about':
require_once __DIR__ . '/../src/controllers/AboutController.php';
$controller = new AboutController();
$controller->show();
break;
default:
http_response_code(404);
echo "404 - Page Not Found";
break;
}
src/controllers/HomeController.php<?php
class HomeController {
public function index() {
// Load a view file (HTML template with PHP logic)
require_once __DIR__ . '/../views/home.php';
}
}
/src Directly?Technically, you can if you:
/src where users can call them directly (e.g., http://example.com/src/helpers/Utils.php).Inconsistent Organization:
/src files directly, you lose the ability to centralize routing and control how requests are handled.Maintenance Challenges:
Security Concerns:
/src are not designed to be entry points and might expose sensitive logic or functionality if accessed directly.If your project is for internal use only (e.g., accessed by trusted users), you can still follow best practices to maintain structure and security. Here's what you can do:
Restrict Access to /src via .htaccess or Server Config:
/src entirely but allow PHP scripts to include these files.Example .htaccess:
<Directory "/path-to-project/src">
Order Deny,Allow
Deny from all
</Directory>
For Nginx:
location /src {
deny all;
}
Authenticate Users:
Use a Single Entry Point for All Requests:
index.php for consistency./src. These files should contain internal logic and helpers, not public-facing code.index.php in the public/ folder.
Lem - Warren Buffett walked into a University of Florida classroom in 1998 ... [2098423133938356224] (1)
Sam Stoffel - Jack Dorsey's simple rule for success in business [2089319019065057280]
Peter-Steinberger-What-Happens-When-4.7-_1786407922546
Monitor_server_action____
Disk_and_RAID_testing_commandsWebDev New York London AI NORWAY DIY Ideas Life Family News Clothing Invest Sports Health