The Minimal PHP Framework for Pragmatic Developers

Only 1.3 MB. Learn in 10 minutes.

PHPSSP really helpful for you to build a simple website or web application. Sometimes i can just install Laravel or Codeigniter, but i think its too heavy for simple website. So i build PHPSSP for simple website or web application.

I just need a controller, model, view, routes, input sanitization, database connection, and xss protection. Pretty simple, if you need a custom package you can install it with composer.

PHPSSP creator is MavenTama

Installation

PHPSSP is a Simple Framework for PHP. It is a simple and easy to use framework for PHP, its fast and lightweight. It is designed to be simple and easy to use. Installation you can use git clone or composer.

composer create-project maventama/phpssp

or

git clone https://github.com/maventama/phpssp.git

Then composer install

composer install

Run server

php -S localhost:8000 -t public

PHPSSP really helpful for you to build a simple website or web application. Sometimes i can just install Laravel or Codeigniter, but i think it's too heavy for simple website. So i build PHPSSP for simple website or web application.

I just need a controller, model, view, routes, input sanitization, database connection, and xss protection. Pretty simple, if you need a custom package you can install it with composer.

Features

  • Simple and easy to use
  • Fast and lightweight
  • Easy to extend
  • Easy to customize
  • Controllers
  • Models
  • Views
  • Routing
  • Dynamic Routing
  • Middleware
  • Rate Limiter
  • CSRF Protection
  • Input Sanitization

Environtment

Look its pretty simple, you just need to setup your database environtment in .env file. If you have a multiple connections, you can add it to custom without crazy inheritance grandparent.

DB_HOST=localhost
DB_NAME=db_name
DB_USER=your_user
DB_PASS=y0ur_p4ssw0rd

RATE_LIMITER_MAX_REQUESTS=100
RATE_LIMITER_TIME_WINDOW=60

Additional Configuration

  • If you have multiple connections, you can add like DB_HOST_2, DB_NAME_2, DB_USER_2, DB_PASS_2. Then, in config.php you can import it with env('DB_HOST_2').
  • Then you can create Database2.php in app/library, and your model just import Database2.php

MVC

Let's talk about MVC in this framework, it's look a like Laravel or Codeigniter. You will not confuse and headache.

Model

In PHPSSP, we don't have a ORM, you can use PDO or Query Builder. You can create a model in app/models, and you can use it in your controller. Inside your model, you can setup every query you need.

<?php
// app/models/UserModel.php

require_once __DIR__ . '/../Library/Database.php';
require_once __DIR__ . '/../models/Model.php';

class UserModel extends Model {
    public function getAll()
    {
        $users = $this->db->query("SELECT * FROM users");
    }
}

Controller

In PHPSSP, you can create a controller in app/controllers, and you can use it in your routes. Inside your controller, you can call your model, request, response, and view.

<?php
// app/controllers/HomeController.php

require_once __DIR__ . '/Controller.php';
require_once __DIR__ . '/../models/UserModel.php';

class HomeController extends Controller {
    private $db;

    public function __construct() {
        $config = require __DIR__ . '/../config.php';
        $this->db = new Database($config);
    }
    public function index() {
        $data = ['title' => 'Home Page', 'message' => 'Ini adalah halaman home.'];
        $this->view('home', $data);
    }

    public function about() {
        $data = ['title' => 'About Page', 'message' => 'Ini adalah halaman about.'];
        $this->view('home', $data);
    }
}

It'sp pretty cool and simple right?

View

In PHPSSP, you can create a view in views, and you can use it in your controller. Inside your view, you can setup your html, css, and javascript.

<!-- views/home.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?= e($title); ?></title>
</head>
<body>
    <h1><?= e($title); ?></h1>
    <p><?= e($message); ?></p>
</body>
</html>

Routing & Middleware

Routing

For you who familiar with Laravel, you will not confuse with this routing. You can create a routing in routes/web.php, and you can use it in your controller. Inside your routing, you can setup your route, controller, middleware, and method.

<?php
// routes/web.php

require_once __DIR__ . '/../app/library/Router.php';

$router = new Router();

$router->add('GET', '/', 'HomeController@index', ['RateLimiter', 'InputSanitizer']);
$router->add('GET', '/about', 'HomeController@about', ['RateLimiter', 'InputSanitizer']);
$router->add('POST', '/submit-form', 'FormController@submit', ['RateLimiter', 'InputSanitizer']);

$router->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

Middleware

By default we provide Rate Limiter and Input Sanitizer. You can create a middleware in app/middleware, and you can use it in your routes. Inside your middleware, you can setup your middleware.

<?php

class InputSanitizer {
    public function handle($request) {
        foreach ($_POST as $key => $value) {
            $_POST[$key] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
        }

        foreach ($_GET as $key => $value) {
            $_GET[$key] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
        }
    }
}

Helper

Look if you have a helper function, you can create a helper in app/helper.php, and you can use it in your controller, model, or view. Inside your helper, you can setup your helper function.

<?php
// app/helper.php

require_once __DIR__ . '/Library/CSRF.php';
function e($string) {
    return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
function csrf_token() {
    return CSRF::generateToken();
}

CSRF Protection

By default we provide CSRF Protection. You can use it in your form. Inside your form, you can setup your csrf token.

<form action="/submit-form" method="post">
    <input type="hidden" name="csrf_token" value="<?= csrf_token(); ?>">
    <input type="text" name="name">
    <input type="submit" value="Submit">
</form>

Rate Limiter

You can set your rate limiter in .env file. You can setup your rate limiter in your middleware. Inside your middleware, you can setup your rate limiter.

RATE_LIMITER_MAX_REQUESTS=100 # 100 requests
RATE_LIMITER_TIME_WINDOW=60 # 60 seconds

Ok, Just Like That

It's pretty simple right? You can use PHPSSP for your simple website or web application. If you have a question, you can ask me on Github.

If you have an idea, you can contribute to this project. I'm happy if you want to contribute to this project.