268 lines
2.8 KiB
Markdown
Executable File
268 lines
2.8 KiB
Markdown
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:
|
|
|
|
```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
|
|
```
|
|
|
|
---
|
|
|
|
## Running
|
|
|
|
Start the development server:
|
|
|
|
```bash
|
|
./server.sh
|
|
```
|
|
|
|
Default URL:
|
|
|
|
```text
|
|
http://127.0.0.1:3333
|
|
```
|
|
|
|
---
|
|
|
|
## Routes
|
|
|
|
Routes are defined in:
|
|
|
|
```php
|
|
config/routes.php
|
|
```
|
|
|
|
Example:
|
|
|
|
```php
|
|
return [
|
|
'GET' => [
|
|
'/' => 'home.php',
|
|
'/about' => 'about.php',
|
|
'/api/test' => 'api/example.php',
|
|
],
|
|
];
|
|
```
|
|
|
|
Routes beginning with:
|
|
|
|
```text
|
|
api/
|
|
```
|
|
|
|
are loaded from:
|
|
|
|
```text
|
|
src/api/
|
|
```
|
|
|
|
Everything else is loaded from:
|
|
|
|
```text
|
|
src/views/pages/
|
|
```
|
|
|
|
---
|
|
|
|
## Adding A New Page
|
|
|
|
### 1. Create the page
|
|
|
|
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');
|
|
```
|
|
|
|
### 2. Add the route
|
|
|
|
Edit:
|
|
|
|
```php
|
|
config/routes.php
|
|
```
|
|
|
|
Add:
|
|
|
|
```php
|
|
'GET' => [
|
|
'/contact' => 'contact.php',
|
|
],
|
|
```
|
|
|
|
Visit:
|
|
|
|
```text
|
|
http://localhost:3333/contact
|
|
```
|
|
|
|
---
|
|
|
|
## Adding An API Endpoint
|
|
|
|
### 1. Create the endpoint
|
|
|
|
Create:
|
|
|
|
```text
|
|
src/api/contact.php
|
|
```
|
|
|
|
Example:
|
|
|
|
```php
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(['success' => true]);
|
|
```
|
|
|
|
### 2. Add the route
|
|
|
|
```php
|
|
'GET' => [
|
|
'/api/contact' => 'api/contact.php',
|
|
],
|
|
```
|
|
|
|
Visit:
|
|
|
|
```text
|
|
http://localhost:8000/api/contact
|
|
```
|
|
|
|
---
|
|
|
|
## SQLite
|
|
|
|
Database configuration:
|
|
|
|
```php
|
|
config/database.php
|
|
```
|
|
|
|
Usage:
|
|
|
|
```php
|
|
$db = db();
|
|
|
|
$rows = $db->query('SELECT * FROM users')->fetchAll();
|
|
```
|
|
|
|
---
|
|
|
|
## Helpers
|
|
|
|
Available helpers:
|
|
|
|
```php
|
|
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
|