Route2/README.md
2025-04-13 17:48:40 +02:00

108 lines
2.5 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 middleware
Route::before(function () {
echo "Middleware executed before the route callback.";
});
// Define a POST route with middleware
Route::post('/submit', function () {
echo "Form submitted!";
}, function () {
echo "Middleware executed before the callback.";
});
// Group routes under a common prefix
Route::group('/admin', function () {
// Define a middleware for the group; it will not affect the outer routes
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
```PHP
use WilliamAAK\Http\Route;
Route::get('/', fn() => print('hello world'));
```
See `Route.php` and read the comments for more information
## Access directly
The simplest way to access your routes. Just put the file in your favorite folder and run it!
For example
`http://your.site/folder/index.php/your/route`
## Rewrite requests
The more sexy way of doing it.
For example
`http://your.site/your/route`
### 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.
location / {
try_files $uri $uri/ /index.php?$query_string;
}