Added init script + update Readme with its details.
This commit is contained in:
@@ -1,21 +1,29 @@
|
|||||||
# miniPHP
|
# miniPHP
|
||||||
|
|
||||||
A tiny PHP starter project for small websites and personal projects.
|
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:
|
The goal is simple:
|
||||||
|
|
||||||
* Clone
|
1. Clone the repository
|
||||||
* Change git remote
|
2. Run `init_miniphp.sh`
|
||||||
* Start building
|
3. Start building
|
||||||
|
|
||||||
No framework.
|
If a feature can be implemented with a few lines of plain PHP, prefer that approach.
|
||||||
No Composer.
|
|
||||||
No external dependencies.
|
|
||||||
No ORM.
|
|
||||||
No dependency injection.
|
|
||||||
No magic.
|
|
||||||
|
|
||||||
Just PHP.
|
miniPHP exists for developers who want:
|
||||||
|
|
||||||
|
* A clean starting point
|
||||||
|
* Full control over their code
|
||||||
|
* Zero framework lock-in
|
||||||
|
* Zero external dependencies
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -25,20 +33,54 @@ Just PHP.
|
|||||||
* No Composer required
|
* No Composer required
|
||||||
* Simple config-based router
|
* Simple config-based router
|
||||||
* Page and API endpoints
|
* Page and API endpoints
|
||||||
|
* Route prefix support
|
||||||
* SQLite support via PDO
|
* SQLite support via PDO
|
||||||
* Shared partials
|
* Shared view partials
|
||||||
* Secure path traversal protection
|
|
||||||
* Helper functions
|
* Helper functions
|
||||||
|
* Path traversal protection
|
||||||
* Development server script
|
* 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
|
## Requirements
|
||||||
|
|
||||||
* PHP 8.1+
|
* PHP 8.1+
|
||||||
* SQLite extension (optional)
|
* PDO SQLite extension (optional)
|
||||||
|
|
||||||
Check installed extensions:
|
View installed extensions:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
php -m
|
php -m
|
||||||
@@ -57,7 +99,7 @@ public/
|
|||||||
├── css/
|
├── css/
|
||||||
│ └── app.css
|
│ └── app.css
|
||||||
└── js/
|
└── js/
|
||||||
└── app.js
|
│ └── app.js
|
||||||
|
|
||||||
src/
|
src/
|
||||||
├── api/
|
├── api/
|
||||||
@@ -74,6 +116,7 @@ storage/
|
|||||||
|
|
||||||
index.php
|
index.php
|
||||||
server.sh
|
server.sh
|
||||||
|
init_miniphp.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -94,7 +137,7 @@ http://127.0.0.1:3333
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Routes
|
## Routing
|
||||||
|
|
||||||
Routes are defined in:
|
Routes are defined in:
|
||||||
|
|
||||||
@@ -107,26 +150,22 @@ Example:
|
|||||||
```php
|
```php
|
||||||
return [
|
return [
|
||||||
'GET' => [
|
'GET' => [
|
||||||
'/' => 'home.php',
|
'/' => 'home.php',
|
||||||
'/about' => 'about.php',
|
'/about' => 'about.php',
|
||||||
'/api/test' => 'api/example.php',
|
'/api/test' => 'api/example.php',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
## Route Prefixes
|
### Route Prefixes
|
||||||
|
|
||||||
Routes can be mapped to different source directories using prefixes.
|
By default page routes resolve to:
|
||||||
|
|
||||||
By default:
|
|
||||||
|
|
||||||
```text
|
```text
|
||||||
src/views/pages/
|
src/views/pages/
|
||||||
```
|
```
|
||||||
|
|
||||||
is used for normal pages.
|
Additional route prefixes can be registered:
|
||||||
|
|
||||||
The router also supports configurable route prefixes:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$route_bases = [
|
$route_bases = [
|
||||||
@@ -158,7 +197,7 @@ loads:
|
|||||||
src/views/pages/about.php
|
src/views/pages/about.php
|
||||||
```
|
```
|
||||||
|
|
||||||
### Adding A New Route Type
|
### Creating New Route Types
|
||||||
|
|
||||||
Create a new directory:
|
Create a new directory:
|
||||||
|
|
||||||
@@ -166,7 +205,7 @@ Create a new directory:
|
|||||||
src/docs/
|
src/docs/
|
||||||
```
|
```
|
||||||
|
|
||||||
Then add a prefix:
|
Register it:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$route_bases = [
|
$route_bases = [
|
||||||
@@ -181,19 +220,17 @@ Now:
|
|||||||
'/docs/getting-started' => 'docs/getting-started.php',
|
'/docs/getting-started' => 'docs/getting-started.php',
|
||||||
```
|
```
|
||||||
|
|
||||||
will load:
|
resolves to:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
src/docs/getting-started.php
|
src/docs/getting-started.php
|
||||||
```
|
```
|
||||||
|
|
||||||
No router changes required.
|
No router modifications required.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Adding A New Page
|
## Pages
|
||||||
|
|
||||||
### 1. Create the page
|
|
||||||
|
|
||||||
Create:
|
Create:
|
||||||
|
|
||||||
@@ -210,20 +247,10 @@ Example:
|
|||||||
|
|
||||||
<p>Get in touch.</p>
|
<p>Get in touch.</p>
|
||||||
|
|
||||||
<?php
|
<?php partial('footer'); ?>
|
||||||
|
|
||||||
partial('footer');
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Add the route
|
Register the route:
|
||||||
|
|
||||||
Edit:
|
|
||||||
|
|
||||||
```php
|
|
||||||
config/routes.php
|
|
||||||
```
|
|
||||||
|
|
||||||
Add:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
'GET' => [
|
'GET' => [
|
||||||
@@ -239,9 +266,7 @@ http://localhost:3333/contact
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Adding An API Endpoint
|
## API Endpoints
|
||||||
|
|
||||||
### 1. Create the endpoint
|
|
||||||
|
|
||||||
Create:
|
Create:
|
||||||
|
|
||||||
@@ -258,10 +283,10 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
echo json_encode(['success' => true]);
|
echo json_encode([ 'success' => true, ]);
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Add the route
|
Register the route:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
'GET' => [
|
'GET' => [
|
||||||
@@ -269,17 +294,11 @@ echo json_encode(['success' => true]);
|
|||||||
],
|
],
|
||||||
```
|
```
|
||||||
|
|
||||||
Visit:
|
|
||||||
|
|
||||||
```text
|
|
||||||
http://localhost:8000/api/contact
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## SQLite
|
## Database
|
||||||
|
|
||||||
Database configuration:
|
Configuration:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
config/database.php
|
config/database.php
|
||||||
@@ -290,14 +309,14 @@ Usage:
|
|||||||
```php
|
```php
|
||||||
$db = db();
|
$db = db();
|
||||||
|
|
||||||
$rows = $db->query('SELECT * FROM users')->fetchAll();
|
$rows = $db->query( 'SELECT * FROM users')->fetchAll();
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Helpers
|
## Helpers
|
||||||
|
|
||||||
Available helpers:
|
Included helpers:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
escape_string()
|
escape_string()
|
||||||
@@ -309,16 +328,7 @@ 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
|
## License
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
|
||||||
|
|||||||
Executable
+57
@@ -0,0 +1,57 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "================================="
|
||||||
|
echo " MiniPHP Initializer"
|
||||||
|
echo "================================="
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Remove git remote if it exists
|
||||||
|
if git remote get-url origin >/dev/null 2>&1; then
|
||||||
|
git remote remove origin
|
||||||
|
echo "[✓] Removed existing git remote 'origin'"
|
||||||
|
else
|
||||||
|
echo "[i] No git remote 'origin' found"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Gather README information
|
||||||
|
read -rp "Project title: " PROJECT_TITLE
|
||||||
|
read -rp "Project description: " PROJECT_DESCRIPTION
|
||||||
|
|
||||||
|
# Create README
|
||||||
|
cat > README.md << EOF
|
||||||
|
# ${PROJECT_TITLE}
|
||||||
|
|
||||||
|
${PROJECT_DESCRIPTION}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo "[✓] Generated README.md"
|
||||||
|
echo
|
||||||
|
echo "================================="
|
||||||
|
echo " Next Steps"
|
||||||
|
echo "================================="
|
||||||
|
echo
|
||||||
|
echo "Create a new repository and add it as the remote:"
|
||||||
|
echo
|
||||||
|
echo " git remote add origin <your-repo-url>"
|
||||||
|
echo " git branch -M main"
|
||||||
|
echo " git push -u origin main"
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Optional self-delete
|
||||||
|
read -rp "Delete init_miniphp.sh? [y/N]: " DELETE_SCRIPT
|
||||||
|
case "$DELETE_SCRIPT" in
|
||||||
|
[yY]|[yY][eE][sS])
|
||||||
|
SCRIPT_PATH="$(realpath "$0")"
|
||||||
|
echo "[✓] Removing $SCRIPT_PATH"
|
||||||
|
rm -f "$SCRIPT_PATH"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "[i] Keeping init_miniphp.sh"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "[✓] Initialization complete"
|
||||||
Reference in New Issue
Block a user