A lightweight, modern MVC framework for PHP applications with built-in authentication, database abstraction, form validation, and more.
This PHP-MVC framework provides a structured architecture for building web applications following the Model-View-Controller pattern. It includes essential features like routing, database interactions, form handling, authentication, and session management.
- MVC Architecture: Clean separation of concerns with Models, Views, and Controllers
- Routing System: Simple and flexible URL routing
- Database Abstraction: PDO-based database interactions with migration support
- Form Handling: Built-in form validation and rendering
- Authentication: User registration, login, and access control
- Session Management: Secure session handling with flash messages
- Middleware Support: Filter requests before they reach controllers
- Validation Rules: Extensive validation for form inputs
- Layouts System: Reusable view layouts and components
- PHP 8.0+
- PDO PHP Extension
- MySQL/MariaDB
-
Clone the repository:
git clone https://github.com/Grizi7/PHP-MVC.git
-
Install dependencies via Composer:
composer install
-
Create a
.envfile (use.env.exampleas template):DB_DSN = mysql:host=localhost;port=3306;dbname=your_database DB_USER = root DB_PASSWORD = your_password -
Run database migrations:
php migrations.php
-
Configure your web server to point to the
publicdirectory.
PHP-MVC/
├── controllers/ # Application controllers
├── core/ # Framework core components
│ ├── form/ # Form handling classes
│ ├── middlewares/ # Middleware components
│ └── exceptions/ # Custom exceptions
├── migrations/ # Database migrations
├── models/ # Application models
├── public/ # Web-accessible files
├── requests/ # Form request validation
├── runtime/ # Runtime data and logs
├── views/ # View templates
│ └── layouts/ # Layout templates
├── .env.example # Environment config template
├── composer.json # Composer dependencies
├── migrations.php # Migration runner
└── README.md # Project documentation
Define routes in public/index.php:
$app->router->get('/users', [UserController::class, 'index']);
$app->router->post('/users/create', [UserController::class, 'create']);namespace app\controllers;
use app\core\Controller;
use app\core\Request;
class UserController extends Controller
{
public function index(): string
{
return $this->render('users/index', [
'users' => User::findAll()
]);
}
public function create(Request $request): string
{
// Handle form submission...
}
}namespace app\models;
use app\core\DBModel;
class User extends DBModel
{
public static string $tableName = 'users';
public function attributes(): array
{
return ['name', 'email', 'password'];
}
public function rules(): array
{
return [
'name' => [self::RULE_REQUIRED],
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [self::RULE_UNIQUE, 'class' => self::class]]
];
}
}Views are PHP files in the views directory:
<!-- views/users/index.php -->
<h1>Users</h1>
<div class="user-list">
<?php foreach ($users as $user): ?>
<div class="user-item">
<?= htmlspecialchars($user->name) ?>
</div>
<?php endforeach; ?>
</div>The framework provides a simple way to create and validate forms:
<?php $form::begin('register', 'post') ?>
<div class="row">
<div class="col">
<?= $form->field($model, 'first_name') ?>
</div>
<div class="col">
<?= $form->field($model, 'last_name') ?>
</div>
</div>
<?= $form->field($model, 'email', 'email') ?>
<?= $form->field($model, 'password', 'password') ?>
<?php $form::end() ?>The framework includes built-in authentication with registration, login, and profile management:
// Login a user
$user = new User();
$user->email = $email;
$user->password = $password;
$user->login();
// Check authentication
if (isGuest()) {
// Handle unauthenticated user
}Middleware can be used to filter HTTP requests:
// Register middleware in a controller
public function __construct()
{
$this->registerMiddleware(new AuthMiddleware(['profile']));
}The framework includes useful helper functions:
// Redirect to a different URL
redirect('/home');
// Set flash message
sessionFlashSet('success', 'Operation completed successfully');
// Get authenticated user
$currentUser = user();Create a new migration:
- Create a file in the
migrationsdirectory (e.g.,m_004_create_posts.php):
<?php
use app\core\Application;
class m_004_create_posts
{
public function up()
{
$db = Application::$app->db;
$SQL = "CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
user_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=INNODB;";
$db->pdo->exec($SQL);
}
public function down()
{
$db = Application::$app->db;
$SQL = "DROP TABLE IF EXISTS posts;";
$db->pdo->exec($SQL);
}
}- Run the migrations:
php migrations.phpMIT License
Contributions are welcome! Please feel free to submit a Pull Request.
Grizi7 Portfolio