2023-03-21 20:41:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use WillySoft\Route as App;
|
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
// generated by composer, if you are not using composer include
|
|
|
|
// the file in a different manner
|
|
|
|
// require __DIR__ . '/edit/this/location/file.php';
|
2023-03-21 20:41:02 +00:00
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
//-----------------------------------------------------
|
|
|
|
// Basic Routing
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// there are shorthands for standard HTTP request methods such as
|
2023-03-21 20:41:02 +00:00
|
|
|
// get|post|put|patch|delete|options
|
2024-11-13 17:11:32 +00:00
|
|
|
App::get('/', function() {
|
|
|
|
?>
|
|
|
|
<h1>Homepage!</h1>
|
|
|
|
<p>Welcome to the homepage</p>
|
|
|
|
<?php
|
|
|
|
});
|
2024-10-31 21:40:23 +00:00
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
// form is a shorthand that accepts GET and POST request methods
|
2023-03-21 20:41:02 +00:00
|
|
|
App::form('/submit',
|
|
|
|
fn() => print('i match on GET and POST methods'));
|
|
|
|
|
2024-10-31 21:40:23 +00:00
|
|
|
// if not satisfied you can use the match function that takes a
|
2023-03-21 20:41:02 +00:00
|
|
|
// string of methods separated by the pipe symbol
|
|
|
|
App::match('get|post|put', '/match',
|
|
|
|
fn() => print('i match on any method you like'));
|
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
// "any" is a shorthand allowing all HTTP request methods to pass
|
|
|
|
App::any('/example',
|
|
|
|
fn() => print('i match on any method'));
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// Route Parameters
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// capture segments of the URL_PATH within your route. for example,
|
|
|
|
// you may need to capture a users ID. you may do so by defining
|
|
|
|
// route parameters.
|
|
|
|
// parameters are injected into route callbacks based on their order.
|
2023-03-21 20:41:02 +00:00
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
// a required parameter will only match when a value is supplied
|
|
|
|
App::get('/echo/$text',
|
2023-03-21 20:41:02 +00:00
|
|
|
fn($text) => print($text));
|
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
// optional parameter, only the last parameter can be optional
|
|
|
|
// or it will throw an exception
|
|
|
|
App::get('/echo_optional/$text?',
|
|
|
|
fn($text = 'You sent nothing') => print($text));
|
|
|
|
|
|
|
|
// crude friends list example
|
|
|
|
App::get('/user/$user_id/friends/$pagination?', function($user_id, $pagination = '0') {
|
|
|
|
?>
|
|
|
|
<h1>Showing friends for user: <?=$user_id?></h1>
|
|
|
|
<p>Current page: <?=$pagination?></p>
|
|
|
|
<?php
|
|
|
|
});
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// Middlewares
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// middlewares provide a convenient mechanism for inspecting and
|
|
|
|
// filtering requests. you can imagine them as a series of layers
|
|
|
|
// that requests must pass through before they hit your application.
|
|
|
|
// a layer can be used for auth, rate limiting or anything really
|
|
|
|
App::use(
|
|
|
|
fn() => print('<p>Hello all routes!</p>'));
|
|
|
|
|
|
|
|
//-----------------------------------------------------
|
|
|
|
// Route Groups
|
|
|
|
//-----------------------------------------------------
|
2023-03-21 20:41:02 +00:00
|
|
|
// group together routes and middlewares. a prefix can be added to
|
2024-11-13 17:11:32 +00:00
|
|
|
// prefix each route in the group with a given URL_PATH. the group
|
|
|
|
// will be skipped if the request URL_PATH does not start with the
|
|
|
|
// one supplied. middlewares defined in here will only run on routes
|
|
|
|
// matched from within or any child groups
|
2023-03-21 20:41:02 +00:00
|
|
|
App::group('/test', function() {
|
|
|
|
App::use(
|
|
|
|
fn() => print('<p>Hello all routes matched within this or any child groups!</p>'));
|
|
|
|
|
2024-11-13 17:11:32 +00:00
|
|
|
// this will be seen as /test/
|
2023-03-21 20:41:02 +00:00
|
|
|
App::get('/',
|
|
|
|
fn() => print('Testing 123'));
|
|
|
|
});
|
|
|
|
|
|
|
|
// finally, since no route was matched, show a 404 page
|
|
|
|
http_response_code(404);
|
|
|
|
?>
|
|
|
|
<h1>404 not found</h1>
|
|
|
|
<p>Sorry, the page you requested could not be found</p>
|