Route2/README.md
2025-04-14 22:05:06 +02:00

140 lines
3.6 KiB
Markdown

# Route
A lightweight routing system for handling HTTP requests.
### Features:
- Define routes for specific HTTP methods (GET, POST, PUT, DELETE, etc.).
- Apply middleware to routes or groups of routes.
- Group routes under a common prefix for better organization.
- Support for route parameters (e.g., `/user/$id`) and optional parameters (e.g., `/user/$id?`).
### Example:
```php
// Define a GET route
Route::get('/home', function () {
echo "Welcome to the homepage!";
});
// 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 route with a middleware attached to it
Route::post('/submit', function () {
echo "Form submitted!";
}, function () {
echo "Middleware executed before the callback.";
});
// Group routes under a common prefix
Route::group('/admin', function () {
// Middlewares defined here will not affect the routes outside this group
Route::before(function () {
echo "Admin Middleware executed.";
});
Route::get('/dashboard', function () {
echo "Admin Dashboard";
});
Route::post('/settings', function () {
echo "Admin Settings";
});
});
```
# Installation
## 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
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/WilliamAAK/Route"
}
],
"require": {
"williamaak/route": "dev-master"
}
}
```
Fetch the package by running
composer install
## Manual
Its all in a single file; include it in your project like so.
```php
require '/YourPath/Route.php'
```
# Usage
To get started quickly you may copy this into your project.
```php
use WilliamAAK\Http\Route;
Route::get('/$world?', function($world = 'World') {
echo "Hello, $world!";
});
// since no route was matched show a 404 page
http_response_code(404);
?>
<h1>Page not found!</h1>
```
## Access directly
The simplest way to access your routes, put the file in your favorite folder and run it!
If you request `http://your.site/yourscript.php/your/route` the route will be automatically converted to `/your/route`
## Rewrite requests
Want to hide that pesky script name (e.g `index.php`) from the URL?
### FrankenPHP
It is recommended that you use FrankenPHP the modern PHP app server. This behavior is enabled by default.
### NGINX
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]
```