Skip to content

itsmutasem/muta-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

384 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Muta Framework

A lightweight, secure, and modern PHP MVC Framework built from scratch.
Designed for learning, rapid prototyping, and small-to-medium web applications.


๐Ÿ“‹ Table of Contents


๐Ÿ“– About

Muta is a handcrafted PHP MVC framework focused on simplicity, security, and clean architecture.

It implements core software engineering patterns including:

  • Front Controller
  • MVC Architecture
  • Middleware Pipeline
  • Dependency Injection
  • Repository Pattern

The goal of Muta is to help developers understand how frameworks work internally while still providing a solid base for building real-world applications.


โœจ Features

Category Features
Architecture MVC Pattern, Front Controller, Dependency Injection Container
Routing Dynamic & static routes, route parameters, HTTP method support
Middleware Composable middleware pipeline
Security CSRF protection, XSS sanitization, rate limiting, prepared statements
Database PDO based ORM, Schema builder, migrations
Validation Server-side form validation with custom rules
Templating Layout based template engine (.muta.php)
CLI Artisan-like CLI scaffolding tools
Authentication One-command login/signup scaffold
Error Handling Environment aware error handling
Environment .env configuration support

๐Ÿ— Architecture Overview

Client Request
      โ”‚
      โ–ผ
.htaccess
      โ”‚
      โ–ผ
Front Controller (public/index.php)
      โ”‚
      โ–ผ
Bootstrap (autoload, env, config)
      โ”‚
      โ–ผ
Router
      โ”‚
      โ–ผ
Middleware Pipeline
      โ”‚
      โ–ผ
Controller
      โ”‚
      โ–ผ
Model
      โ”‚
      โ–ผ
View Rendering
      โ”‚
      โ–ผ
HTTP Response

Design Patterns Used

  • Front Controller โ€” Single application entry point
  • MVC โ€” Separation of concerns
  • Dependency Injection โ€” Automatic dependency resolution
  • Middleware Pipeline โ€” Request filtering
  • Repository Pattern โ€” Clean database access abstraction
  • Template Views โ€” Layout based rendering

๐Ÿ“ Directory Structure

muta/
โ”‚
โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ routes.php
โ”‚   โ”œโ”€โ”€ services.php
โ”‚   โ””โ”€โ”€ middleware.php
โ”‚
โ”œโ”€โ”€ database/
โ”‚   โ””โ”€โ”€ migrations/
โ”‚
โ”œโ”€โ”€ public/
โ”‚   โ”œโ”€โ”€ index.php
โ”‚   โ””โ”€โ”€ .htaccess
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ App/
โ”‚   โ”‚   โ”œโ”€โ”€ Controllers/
โ”‚   โ”‚   โ”œโ”€โ”€ Middleware/
โ”‚   โ”‚   โ””โ”€โ”€ Models/
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ Framework/
โ”‚       โ”œโ”€โ”€ Router.php
โ”‚       โ”œโ”€โ”€ Request.php
โ”‚       โ”œโ”€โ”€ Response.php
โ”‚       โ”œโ”€โ”€ Container.php
โ”‚       โ”œโ”€โ”€ Validator.php
โ”‚       โ””โ”€โ”€ Model.php
โ”‚
โ”œโ”€โ”€ views/
โ”‚
โ”œโ”€โ”€ stubs/
โ”‚
โ”œโ”€โ”€ muta.php
โ”œโ”€โ”€ composer.json
โ””โ”€โ”€ .env

๐Ÿ“ฆ Requirements

  • PHP >= 8.0
  • Composer
  • Apache or Nginx
  • MySQL / MariaDB

๐Ÿ›  Installation

Clone the Repository

git clone https://github.com/yourusername/muta.git
cd muta

Install Dependencies

composer install

Configure Environment

cp .env.example .env

Example .env

DB_HOST=localhost
DB_NAME=muta_db
DB_USER=root
DB_PASS=

APP_ENV=development
APP_DEBUG=true

Run Migrations

php muta.php migrate

๐Ÿ›ค Routing

$router->add('/', [
  'controller' => Home::class,
  'action' => 'index'
]);

$router->add('/products/{id}', [
  'controller' => Products::class,
  'action' => 'show'
]);

$router->add('/products', [
  'controller' => Products::class,
  'action' => 'store',
  'method' => 'POST'
]);

๐ŸŽฎ Controllers

Create controller

php muta.php make:controller ProductController

Example:

class Products extends Controller
{
    public function index(Request $request): Response
    {
        $products = $this->product->findAll();

        return $this->view('Products/index.muta.php', [
            'products' => $products
        ]);
    }
}

๐Ÿ—„ Models & Database

Create model

php muta.php make:model Product

Example model:

class Product extends Model
{
    protected string $table = 'products';
}

Built-in CRUD methods:

  • find()
  • findAll()
  • create()
  • update()
  • delete()

๐ŸŽจ Views & Templating

Two view types:

1. Layout Views (.muta.php)

views/Products/show.muta.php

2. Standalone Views (.php)

Used for emails, error pages, etc.


๐Ÿ”— Middleware

Create middleware:

php muta.php make:middleware AuthenticateMiddleware

Middleware example:

public function process(Request $request, RequestHandlerInterface $handler): Response
{
    if (!isset($_SESSION['user_id'])) {
        return new Response(302, '', ['Location' => '/login']);
    }

    return $handler->handle($request);
}

๐Ÿ“ฆ Dependency Injection Container

Register services:

$container->set(Database::class, function () {
    return new Database(
        $_ENV['DB_HOST'],
        $_ENV['DB_NAME'],
        $_ENV['DB_USER'],
        $_ENV['DB_PASS']
    );
});

Dependencies are automatically resolved in controllers.


โœ… Validation

Example:

$request->validate([
'name' => ['required','min:3'],
'email' => ['required','email'],
'price' => ['numeric']
]);

๐Ÿ”’ Security

Muta includes multiple security layers:

  • CSRF Protection
  • XSS Sanitization
  • SQL Injection Prevention
  • Rate Limiting
  • Server-side Validation
  • Security Headers
  • Output Escaping
  • Secure Error Handling

๐Ÿ–ฅ CLI Commands

php muta.php make:controller UserController
php muta.php make:model User
php muta.php make:middleware AuthMiddleware
php muta.php make:migration create_users_table
php muta.php migrate
php muta.php install:auth

๐Ÿ—ƒ Database Migrations

Create migration:

php muta.php make:migration create_products_table

Run migrations:

php muta.php migrate

๐Ÿ”‘ Authentication Scaffold

Generate authentication system:

php muta.php install:auth

This creates:

  • AuthController
  • User Model
  • Login View
  • Signup View
  • Authentication Middleware

โš  Error Handling

Exception HTTP Code
PageNotFoundException 404
CsrfException 403
Database Exceptions 500
Generic Exceptions 500

๐Ÿ”„ Request Lifecycle

Client
  โ†“
.htaccess
  โ†“
public/index.php
  โ†“
Bootstrap
  โ†“
Router
  โ†“
Middleware
  โ†“
Controller
  โ†“
Model
  โ†“
View
  โ†“
Response

๐Ÿค Contributing

Steps:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to your branch
  5. Open a Pull Request

๐Ÿ“„ License

This project is open source and free to use for personal or commercial applications.



Muta Framework
Simple โ€ข Secure โ€ข Fast

About

Muta (my own framework) - is a lightweight and modern PHP framework designed to make web applicatoin development faster, cleaner, and more secure. It provides a simple yet poweful structure that helps developers focus on writing logic - not boilerplate. Built with clarity and scalability in mind

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors