3.5 KiB
Executable File
miniPHP
A tiny PHP starter project for small websites and personal projects.
The goal is simple:
- Clone
- Change git remote
- Start building
No framework. No Composer. No external dependencies. No ORM. No dependency injection. No magic.
Just PHP.
Features
- Zero dependencies
- No Composer required
- Simple config-based router
- Page and API endpoints
- SQLite support via PDO
- Shared partials
- Secure path traversal protection
- Helper functions
- Development server script
Requirements
- PHP 8.1+
- SQLite extension (optional)
Check installed extensions:
php -m
Project Structure
config/
├── database.php
└── routes.php
public/
├── css/
│ └── app.css
└── js/
└── app.js
src/
├── api/
├── views/
│ ├── pages/
│ └── partials/
├── bootstrap.php
├── database.php
├── helpers.php
└── router.php
storage/
└── database.sqlite
index.php
server.sh
Running
Start the development server:
./server.sh
Default URL:
http://127.0.0.1:3333
Routes
Routes are defined in:
config/routes.php
Example:
return [
'GET' => [
'/' => 'home.php',
'/about' => 'about.php',
'/api/test' => 'api/example.php',
],
];
Route Prefixes
Routes can be mapped to different source directories using prefixes.
By default:
src/views/pages/
is used for normal pages.
The router also supports configurable route prefixes:
$route_bases = [
'api/' => ROOT . '/src/api',
];
Example:
'/api/users' => 'api/users.php',
loads:
src/api/users.php
while:
'/about' => 'about.php',
loads:
src/views/pages/about.php
Adding A New Route Type
Create a new directory:
src/docs/
Then add a prefix:
$route_bases = [
'api/' => ROOT . '/src/api',
'docs/' => ROOT . '/src/docs',
];
Now:
'/docs/getting-started' => 'docs/getting-started.php',
will load:
src/docs/getting-started.php
No router changes required.
Adding A New Page
1. Create the page
Create:
src/views/pages/contact.php
Example:
<?php partial('head', ['title' => 'Contact']); ?>
<h1>Contact</h1>
<p>Get in touch.</p>
<?php
partial('footer');
2. Add the route
Edit:
config/routes.php
Add:
'GET' => [
'/contact' => 'contact.php',
],
Visit:
http://localhost:3333/contact
Adding An API Endpoint
1. Create the endpoint
Create:
src/api/contact.php
Example:
<?php
declare(strict_types=1);
header('Content-Type: application/json');
echo json_encode(['success' => true]);
2. Add the route
'GET' => [
'/api/contact' => 'api/contact.php',
],
Visit:
http://localhost:8000/api/contact
SQLite
Database configuration:
config/database.php
Usage:
$db = db();
$rows = $db->query('SELECT * FROM users')->fetchAll();
Helpers
Available helpers:
escape_string()
debug()
debugx()
get_config()
partial()
Philosophy
miniPHP intentionally avoids framework features.
If a feature can be implemented with plain PHP and a few lines of code, prefer that approach.
The goal is to remain small, understandable, and easy to clone for future projects.
License
MIT