From d60e072010f0991991956fab9add12672b5a3c9b Mon Sep 17 00:00:00 2001 From: William Date: Tue, 21 Mar 2023 21:41:02 +0100 Subject: [PATCH] Init --- LICENSE | 13 ++++++++ README.md | 37 ++++++++++++++++++++++ Route.php | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 21 ++++++++++++ example.php | 64 +++++++++++++++++++++++++++++++++++++ 5 files changed, 223 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Route.php create mode 100644 composer.json create mode 100644 example.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a086273 --- /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..6bf9bec --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Route + +Minimal PHP Router + +# Installation + +## Composer + +Composer needs to know where to locate the package. Let's add the repository to your project by declaring it in the `composer.json` file. +```JSON +{ + "repositories": [ + { + "type": "vcs", + "url": "https://git.willy.club/WillySoft/Route" + } + ], + "require": { + "willysoft/route": "dev-master" + } +} +``` +Fetch the package by running + + composer install + +# Usage + +When using a production web server such as Nginx you will have to tell it to rewrite requests that don't point to a local file, such as they can be forwarded to your front controller which in this case is `public/index.php`. + +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; + } + +See `example.php` and add it to your `public/index.php` file. It will act as the front controller. \ No newline at end of file diff --git a/Route.php b/Route.php new file mode 100644 index 0000000..f6a340e --- /dev/null +++ b/Route.php @@ -0,0 +1,88 @@ +=8.1.0" + }, + "license": "MIT", + "authors": [ + { + "name": "noreply", + "email": "noreply@willy.club" + } + ], + "minimum-stability": "dev", + "autoload": { + "psr-4": { + "WillySoft\\": "./" + } + } +} \ No newline at end of file diff --git a/example.php b/example.php new file mode 100644 index 0000000..aaa8d00 --- /dev/null +++ b/example.php @@ -0,0 +1,64 @@ + print('

Hello all routes!

')); + +// there are shorthands for methods you would expect such as +// get|post|put|patch|delete|options +App::get('/', + fn() => print('homepage')); + +// form is a shorthand that accepts GET and POST methods +App::form('/submit', + fn() => print('i match on GET and POST methods')); + +// any is a shorthand that does what you would expect +App::any('/example', + fn() => print('i match on any method')); + +// if not satisfied you can use the match function which takes a +// string of methods separated by the pipe symbol +App::match('get|post|put', '/match', + fn() => print('i match on any method you like')); + +// optional route parameters +App::get('/echo/$text?', + fn($text = 'You sent nothing') => print($text)); + +// required route parameters +App::get('/echo_must_supply_text/$text', + fn($text) => print($text)); + +// group together routes and middlewares. a prefix can be added to +// prefix each route in the group with a given URI. the group will +// be skipped if the request URI does not begin with the one supplied. +// middlewares defined in here will only affect routes matched from +// within or any child groups +App::group('/test', function() { + App::use( + fn() => print('

Hello all routes matched within this or any child groups!

')); + + // this will be matched as /test/ + App::get('/', + fn() => print('Testing 123')); + + // you may also define a group without a prefix + App::group(callback: function() { + App::get('/test', + fn() => print('Testing 456')); + }); +}); + +// finally, since no route was matched, show a 404 page +http_response_code(404); +?> +

404 not found

+

Sorry, the page you requested could not be found