335 lines
3.9 KiB
Markdown
Executable File
335 lines
3.9 KiB
Markdown
Executable File
# miniPHP
|
|
|
|
A tiny PHP project template for small websites, internal tools, APIs, and personal projects.
|
|
|
|
miniPHP is not a framework.
|
|
|
|
There is no Composer requirement, no dependency injection container, no ORM, no service providers, and no framework lifecycle to learn.
|
|
|
|
Just plain PHP with a simple router, helper functions, optional SQLite support, and a project structure that stays out of your way.
|
|
|
|
## Philosophy
|
|
|
|
The goal is simple:
|
|
|
|
1. Clone the repository
|
|
2. Run `init_miniphp.sh`
|
|
3. Start building
|
|
|
|
If a feature can be implemented with a few lines of plain PHP, prefer that approach.
|
|
|
|
miniPHP exists for developers who want:
|
|
|
|
* A clean starting point
|
|
* Full control over their code
|
|
* Zero framework lock-in
|
|
* Zero external dependencies
|
|
|
|
---
|
|
|
|
## Features
|
|
|
|
* Zero dependencies
|
|
* No Composer required
|
|
* Simple config-based router
|
|
* Page and API endpoints
|
|
* Route prefix support
|
|
* SQLite support via PDO
|
|
* Shared view partials
|
|
* Helper functions
|
|
* Path traversal protection
|
|
* Development server script
|
|
* Project initialization script
|
|
|
|
---
|
|
|
|
## Quick Start
|
|
|
|
Clone the repository:
|
|
|
|
```bash
|
|
git clone <repository-url> my-project
|
|
cd my-project
|
|
```
|
|
|
|
Initialize the project:
|
|
|
|
```bash
|
|
./init_miniphp.sh
|
|
```
|
|
|
|
The initializer will:
|
|
|
|
* Remove the template git remote
|
|
* Generate a new README
|
|
* Prepare the project for a new repository
|
|
|
|
Then create your own repository and connect it:
|
|
|
|
```bash
|
|
git remote add origin <your-repository-url>
|
|
git push -u origin main
|
|
```
|
|
|
|
Start building.
|
|
|
|
---
|
|
|
|
## Requirements
|
|
|
|
* PHP 8.1+
|
|
* PDO SQLite extension (optional)
|
|
|
|
View installed extensions:
|
|
|
|
```bash
|
|
php -m
|
|
```
|
|
|
|
---
|
|
|
|
## Project Structure
|
|
|
|
```text
|
|
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
|
|
init_miniphp.sh
|
|
```
|
|
|
|
---
|
|
|
|
## Running
|
|
|
|
Start the development server:
|
|
|
|
```bash
|
|
./server.sh
|
|
```
|
|
|
|
Default URL:
|
|
|
|
```text
|
|
http://127.0.0.1:3333
|
|
```
|
|
|
|
---
|
|
|
|
## Routing
|
|
|
|
Routes are defined in:
|
|
|
|
```php
|
|
config/routes.php
|
|
```
|
|
|
|
Example:
|
|
|
|
```php
|
|
return [
|
|
'GET' => [
|
|
'/' => 'home.php',
|
|
'/about' => 'about.php',
|
|
'/api/test' => 'api/example.php',
|
|
],
|
|
];
|
|
```
|
|
|
|
### Route Prefixes
|
|
|
|
By default page routes resolve to:
|
|
|
|
```text
|
|
src/views/pages/
|
|
```
|
|
|
|
Additional route prefixes can be registered:
|
|
|
|
```php
|
|
$route_bases = [
|
|
'api/' => ROOT . '/src/api',
|
|
];
|
|
```
|
|
|
|
Example:
|
|
|
|
```php
|
|
'/api/users' => 'api/users.php',
|
|
```
|
|
|
|
loads:
|
|
|
|
```text
|
|
src/api/users.php
|
|
```
|
|
|
|
while:
|
|
|
|
```php
|
|
'/about' => 'about.php',
|
|
```
|
|
|
|
loads:
|
|
|
|
```text
|
|
src/views/pages/about.php
|
|
```
|
|
|
|
### Creating New Route Types
|
|
|
|
Create a new directory:
|
|
|
|
```text
|
|
src/docs/
|
|
```
|
|
|
|
Register it:
|
|
|
|
```php
|
|
$route_bases = [
|
|
'api/' => ROOT . '/src/api',
|
|
'docs/' => ROOT . '/src/docs',
|
|
];
|
|
```
|
|
|
|
Now:
|
|
|
|
```php
|
|
'/docs/getting-started' => 'docs/getting-started.php',
|
|
```
|
|
|
|
resolves to:
|
|
|
|
```text
|
|
src/docs/getting-started.php
|
|
```
|
|
|
|
No router modifications required.
|
|
|
|
---
|
|
|
|
## Pages
|
|
|
|
Create:
|
|
|
|
```text
|
|
src/views/pages/contact.php
|
|
```
|
|
|
|
Example:
|
|
|
|
```php
|
|
<?php partial('head', ['title' => 'Contact']); ?>
|
|
|
|
<h1>Contact</h1>
|
|
|
|
<p>Get in touch.</p>
|
|
|
|
<?php partial('footer'); ?>
|
|
```
|
|
|
|
Register the route:
|
|
|
|
```php
|
|
'GET' => [
|
|
'/contact' => 'contact.php',
|
|
],
|
|
```
|
|
|
|
Visit:
|
|
|
|
```text
|
|
http://localhost:3333/contact
|
|
```
|
|
|
|
---
|
|
|
|
## API Endpoints
|
|
|
|
Create:
|
|
|
|
```text
|
|
src/api/contact.php
|
|
```
|
|
|
|
Example:
|
|
|
|
```php
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode([ 'success' => true, ]);
|
|
```
|
|
|
|
Register the route:
|
|
|
|
```php
|
|
'GET' => [
|
|
'/api/contact' => 'api/contact.php',
|
|
],
|
|
```
|
|
|
|
---
|
|
|
|
## Database
|
|
|
|
Configuration:
|
|
|
|
```php
|
|
config/database.php
|
|
```
|
|
|
|
Usage:
|
|
|
|
```php
|
|
$db = db();
|
|
|
|
$rows = $db->query( 'SELECT * FROM users')->fetchAll();
|
|
```
|
|
|
|
---
|
|
|
|
## Helpers
|
|
|
|
Included helpers:
|
|
|
|
```php
|
|
escape_string()
|
|
debug()
|
|
debugx()
|
|
get_config()
|
|
partial()
|
|
```
|
|
|
|
---
|
|
|
|
## License
|
|
|
|
MIT
|
|
|