From 25ad36a9aa280a0f3fb6ec664b9edce92c459c63 Mon Sep 17 00:00:00 2001
From: William <54738571+WilliamAAK@users.noreply.github.com>
Date: Mon, 14 Apr 2025 22:05:06 +0200
Subject: [PATCH] Docs: update
---
README.md | 71 ++++++++++++++++++++++++++++++++++++++++---------------
Route.php | 21 ++++++++++++----
2 files changed, 69 insertions(+), 23 deletions(-)
diff --git a/README.md b/README.md
index 4b19ed0..a766aef 100644
--- a/README.md
+++ b/README.md
@@ -15,12 +15,25 @@ Route::get('/home', function () {
echo "Welcome to the homepage!";
});
-// Define a middleware
+// Define a route with a parameter
+Route::get('/user/$id', function ($id) {
+ echo "User ID: $id";
+});
+
+// Define a route with an optional parameter
+Route::get('/user/$id?', function ($id = null) {
+ echo $id ? "User ID: $id" : "No User ID provided.";
+});
+
+// Defining middlewares
Route::before(function () {
echo "Middleware executed before the route callback.";
});
+Route::after(function () {
+ echo "Middleware executed after the route callback.";
+});
-// Define a POST route with middleware
+// Define a route with a middleware attached to it
Route::post('/submit', function () {
echo "Form submitted!";
}, function () {
@@ -29,9 +42,9 @@ Route::post('/submit', function () {
// Group routes under a common prefix
Route::group('/admin', function () {
- // Define a middleware for the group; it will not affect the outer routes
+ // Middlewares defined here will not affect the routes outside this group
Route::before(function () {
- echo "Admin Middleware executed.";
+ echo "Admin Middleware executed.";
});
Route::get('/dashboard', function () {
echo "Admin Dashboard";
@@ -47,7 +60,7 @@ Route::group('/admin', function () {
## Composer
Composer needs to know where to locate the package. Add the repository to your project by declaring it in the `composer.json` file.
-```JSON
+```json
{
"repositories": [
{
@@ -66,42 +79,62 @@ Fetch the package by running
## Manual
-Its all in a single file; include it in your project like so
+Its all in a single file; include it in your project like so.
-```PHP
+```php
require '/YourPath/Route.php'
```
# Usage
-```PHP
+To get started quickly you may copy this into your project.
+
+```php
use WilliamAAK\Http\Route;
-Route::get('/', fn() => print('hello world'));
-```
+Route::get('/$world?', function($world = 'World') {
+ echo "Hello, $world!";
+});
-See `Route.php` and read the comments for more information
+// since no route was matched show a 404 page
+http_response_code(404);
+?>
+
Page not found!
+```
## Access directly
-The simplest way to access your routes. Just put the file in your favorite folder and run it!
+The simplest way to access your routes, put the file in your favorite folder and run it!
-For example
-
-`http://your.site/folder/index.php/your/route`
+If you request `http://your.site/yourscript.php/your/route` the route will be automatically converted to `/your/route`
## Rewrite requests
-The more sexy way of doing it.
+Want to hide that pesky script name (e.g `index.php`) from the URL?
-For example
+### FrankenPHP
-`http://your.site/your/route`
+It is recommended that you use FrankenPHP the modern PHP app server. This behavior is enabled by default.
### NGINX
-With PHP already configured, add this to the server block of your configuration to make requests that don't match a file on your server to be sent to your front controller and begin routing.
+With PHP already installed and configured you may add this to the server block of your configuration to make requests that don't match a file on your server to be redirected to `index.php`
location / {
try_files $uri $uri/ /index.php?$query_string;
}
+
+### Apache
+
+Make sure that mod_rewrite is installed on Apache. On a unix system you can just do
+
+`a2enmod rewrite`
+
+This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to `index.php`
+
+```
+RewriteEngine on
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
+```
\ No newline at end of file
diff --git a/Route.php b/Route.php
index 358765b..35992c8 100644
--- a/Route.php
+++ b/Route.php
@@ -20,12 +20,25 @@ use InvalidArgumentException;
* echo "Welcome to the homepage!";
* });
*
- * // Define a middleware
+ * // Define a route with a parameter
+ * Route::get('/user/$id', function ($id) {
+ * echo "User ID: $id";
+ * });
+ *
+ * // Define a route with an optional parameter
+ * Route::get('/user/$id?', function ($id = null) {
+ * echo $id ? "User ID: $id" : "No User ID provided.";
+ * });
+ *
+ * // Defining middlewares
* Route::before(function () {
* echo "Middleware executed before the route callback.";
* });
+ * Route::after(function () {
+ * echo "Middleware executed after the route callback.";
+ * });
*
- * // Define a POST route with middleware
+ * // Define a route with a middleware attached to it
* Route::post('/submit', function () {
* echo "Form submitted!";
* }, function () {
@@ -34,9 +47,9 @@ use InvalidArgumentException;
*
* // Group routes under a common prefix
* Route::group('/admin', function () {
- * // Define a middleware for the group; it will not affect the outer routes
+ * // Middlewares defined here will not affect the routes outside this group
* Route::before(function () {
- * echo "Admin Middleware executed.";
+ * echo "Admin Middleware executed.";
* });
* Route::get('/dashboard', function () {
* echo "Admin Dashboard";