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
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');
}
}
}
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();
}
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>
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
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.