commit f9162a027fd902805ab1d19a0fe2b9c317b86fd5 Author: William <54738571+WilliamAAK@users.noreply.github.com> Date: Sun Apr 13 17:44:14 2025 +0200 Hello World diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d3f9e2e --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + +Copyright (C) 2004 Whomst Ever + +Everyone is permitted to copy and distribute verbatim or modified +copies of this license document, and changing it is allowed as long +as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f4b4b5 --- /dev/null +++ b/README.md @@ -0,0 +1,107 @@ +# 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": { + "willysoft/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() => echo '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; + } diff --git a/Route.php b/Route.php new file mode 100644 index 0000000..358765b --- /dev/null +++ b/Route.php @@ -0,0 +1,352 @@ +=8.1.0" + }, + "license": "WTFPL", + "authors": [ + { + "name": "WilliamAAK", + "email": "54738571+WilliamAAK@users.noreply.github.com" + } + ], + "minimum-stability": "dev", + "autoload": { + "psr-4": { + "WilliamAAK\\": "./" + } + } +} \ No newline at end of file