feras_altaleb / mvc_php
MVC PHP Small Framework
Package info
github.com/AltalebFeras/template_empty_mvc_for_any_new_project_php_native
pkg:composer/feras_altaleb/mvc_php
Requires
- phpmailer/phpmailer: ^6.9
Requires (Dev)
- phpunit/phpunit: ^12.1
This package is auto-updated.
Last update: 2026-03-21 20:32:50 UTC
README
MVC PHP Framework
Created by Feras Altaleb β for educational purposes only.
A lightweight, extensible MVC framework built with native PHP to help you kickstart web applications quickly and efficiently.
Requires PHP 8.0+ (uses native PHP Attributes for routing).
π Features
- MVC Architecture β Clean separation of concerns using the Model-View-Controller pattern.
- Attribute-Based Routing β Symfony-style
#[Route]attributes declared directly on controller methods. No central routes file needed. - Database Abstraction β Easy database interactions via PDO with a built-in
AbstractRepository. - Security β Session hijacking protection, method spoofing, HTTPS detection, and real IP resolution.
- Extensible β Easily add new controllers; routes are auto-discovered via Reflection.
- Lightweight β Minimal dependencies for optimal performance.
π οΈ Getting Started
1. Clone the Repository
git clone https://github.com/AltalebFeras/template_empty_mvc_for_any_new_project_php_native.git
2. Install Dependencies
composer install
3. Configure Your Environment
- Copy
config_example.phpand rename it toconfig.php. - Update the settings as needed for your environment (development or production):
- Set up your database connection in
config.php. - Set your base URL (
HOME_URL) inconfig.php. - Configure mail settings in
config.php. - Set your timezone in
src/init.php.
- Set up your database connection in
4. Define Your Routes
Routes are defined with the #[Route] attribute directly above each controller method β no central routes file.
use src\Services\Route; class UserController { // Public page β GET only #[Route('/login', methods: ['GET'])] public function showLoginForm(): void { // render login view } // Form submission β POST only #[Route('/login', methods: ['POST'])] public function handleLogin(): void { // process credentials } // Protected page β requires active session #[Route('/dashboard', methods: ['GET'], authRequired: true)] public function showDashboard(): void { // render dashboard view } }
#[Route] parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
$path |
string |
(required) | URL path to match (e.g. '/login') |
$methods |
string|array |
['GET'] |
Allowed HTTP methods |
$name |
string |
'' |
Optional route name |
$authRequired |
bool |
false |
Redirect to /login if not authenticated |
The router auto-discovers every class inside
src/Controllers/β just create a new controller and add#[Route]attributes.
5. HTML Form Method Spoofing
HTML forms only support GET and POST. To send PUT, PATCH, or DELETE, add a hidden field:
<form method="POST" action="/resource/1"> <input type="hidden" name="_method" value="DELETE"> ... </form>
ConfigRouter::getMethod() will resolve the effective method automatically.
6. Run the Application
Access your application in the browser:
http://localhost/path-to-your-project/public
π Directory Structure
public/ # Web root β point your server / virtual host here
β index.php # Single entry point
β .htaccess # URL rewriting (Apache)
βββ assets/ # CSS, JS, images
src/
βββ init.php # Bootstrap: session, autoloader, config, router
βββ Abstracts/
β βββ AbstractController.php # render() and redirect() helpers
β βββ AbstractRepository.php # CRUD helpers (getAll, getById, create, β¦)
βββ Controllers/ # Your controllers β add #[Route] attributes here
βββ Entities/ # Plain PHP entity classes (hydrated via Hydration trait)
βββ Migrations/ # SQL migration files
βββ Repositories/ # Repository classes extending AbstractRepository
βββ Services/
β βββ Route.php # #[Route] PHP attribute definition
β βββ router.php # Auto-discovers and dispatches routes via Reflection
β βββ ConfigRouter.php # HTTP utilities: getMethod, redirect, isAjax, getClientIpβ¦
β βββ Database.php # PDO connection wrapper
β βββ Encrypt_decrypt.php
β βββ Hydration.php # Trait for automatic entity hydration
β βββ Mail.php # PHPMailer wrapper
β βββ Validator.php
βββ Views/ # PHP view templates
π Security Utilities (ConfigRouter)
| Method | Description |
|---|---|
ConfigRouter::getMethod() |
Returns the real HTTP method, supporting PUT/PATCH/DELETE spoofing via _method POST field |
ConfigRouter::checkOriginConnection() |
Validates session IP & user-agent to detect session hijacking β returns false on mismatch |
ConfigRouter::redirect($url, $code) |
Safe redirect with HTTP status code (default 302) |
ConfigRouter::isAjax() |
Detects XMLHttpRequest / fetch requests |
ConfigRouter::isHttps() |
Returns true if the connection is HTTPS |
ConfigRouter::getClientIp() |
Resolves the real client IP (proxy-aware, validated) |
π§° Best Practices
- Use
$authRequired: trueon routes that require a logged-in user. - Never expose the
src/directory β onlypublic/should be the web root. - Store passwords with
password_hash()/password_verify(). - Validate and sanitize all user input at the controller level.
- Test your application thoroughly before deploying to production.
π€ Contributing
Contributions are welcome!
If you have suggestions for improvements or new features, please open an issue or submit a pull request.
π€ Author
Feras Altaleb
GitHub