Compare commits

..

3 Commits

Author SHA1 Message Date
William
c413451500 Docs: update 2025-04-14 22:15:06 +02:00
William
f9b451ef48 Docs: update 2025-04-14 22:13:10 +02:00
William
25ad36a9aa Docs: update 2025-04-14 22:05:06 +02:00
2 changed files with 73 additions and 29 deletions

View File

@ -15,12 +15,25 @@ Route::get('/home', function () {
echo "Welcome to the homepage!"; 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 () { Route::before(function () {
echo "Middleware executed before the route callback."; 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 () { Route::post('/submit', function () {
echo "Form submitted!"; echo "Form submitted!";
}, function () { }, function () {
@ -29,9 +42,9 @@ Route::post('/submit', function () {
// Group routes under a common prefix // Group routes under a common prefix
Route::group('/admin', function () { 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 () { Route::before(function () {
echo "Admin Middleware executed."; echo "Admin Middleware executed.";
}); });
Route::get('/dashboard', function () { Route::get('/dashboard', function () {
echo "Admin Dashboard"; echo "Admin Dashboard";
@ -44,10 +57,10 @@ Route::group('/admin', function () {
# Installation # Installation
## Composer ### Composer:
Composer needs to know where to locate the package. Add the repository to your project by declaring it in the `composer.json` file. 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": [ "repositories": [
{ {
@ -64,44 +77,62 @@ Fetch the package by running
composer install composer install
## Manual ### 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' require '/YourPath/Route.php'
``` ```
# Usage # Usage
```PHP To get started quickly you may copy this into your project.
```php
use WilliamAAK\Http\Route; use WilliamAAK\Http\Route;
Route::get('/', fn() => print('hello world')); 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>
``` ```
See `Route.php` and read the comments for more information The simplest way to access your routes is to put the file in your favorite folder and run it! E.g if you request `http://your.site/yourscript.php/your/route` the route will be automatically converted to `/your/route`
## Access directly # Rewrite requests
The simplest way to access your routes. Just put the file in your favorite folder and run it! Want to hide that pesky script name (e.g `index.php`) from the URL?
For example ### FrankenPHP:
`http://your.site/folder/index.php/your/route` It is recommended that you use FrankenPHP the modern PHP app server. This behavior is enabled by default.
## Rewrite requests ### NGINX:
The more sexy way of doing it. 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`
For example ```nginx
location / {
try_files $uri $uri/ /index.php?$query_string;
}
```
`http://your.site/your/route` ### Apache:
### NGINX Make sure that mod_rewrite is installed on Apache. On a unix system you can just do
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. `a2enmod rewrite`
location / { This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to `index.php`
try_files $uri $uri/ /index.php?$query_string;
} ```apache
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
```

View File

@ -20,12 +20,25 @@ use InvalidArgumentException;
* echo "Welcome to the homepage!"; * 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 () { * Route::before(function () {
* echo "Middleware executed before the route callback."; * 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 () { * Route::post('/submit', function () {
* echo "Form submitted!"; * echo "Form submitted!";
* }, function () { * }, function () {
@ -34,9 +47,9 @@ use InvalidArgumentException;
* *
* // Group routes under a common prefix * // Group routes under a common prefix
* Route::group('/admin', function () { * 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 () { * Route::before(function () {
* echo "Admin Middleware executed."; * echo "Admin Middleware executed.";
* }); * });
* Route::get('/dashboard', function () { * Route::get('/dashboard', function () {
* echo "Admin Dashboard"; * echo "Admin Dashboard";