Route/example.php

60 lines
1.9 KiB
PHP
Raw Normal View History

2023-03-21 20:41:02 +00:00
<?php
use WillySoft\Route as App;
require __DIR__ . '/../vendor/autoload.php';
// middlewares provide a convenient mechanism for inspecting and
2024-10-31 21:40:23 +00:00
// filtering requests. you can imagine them as a series of layers
2023-03-21 20:41:02 +00:00
// 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>'));
// there are shorthands for methods you would expect such as
// get|post|put|patch|delete|options
App::get('/',
fn() => print('homepage'));
2024-10-31 21:40:23 +00:00
// "any" is a shorthand that does what you would expect
// allowing all of the above to pass through
App::any('/example',
fn() => print('i match on any method'));
2023-03-21 20:41:02 +00:00
// form is a shorthand that accepts GET and POST methods
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'));
// 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
2024-10-31 21:40:23 +00:00
// prefix each route in the group with a given PATH. the group will
// be skipped if the requested PATH does not begin with the one supplied.
// middlewares defined in here will only run on routes matched from
2023-03-21 20:41:02 +00:00
// within or any child groups
App::group('/test', function() {
App::use(
fn() => print('<p>Hello all routes matched within this or any child groups!</p>'));
// this will be matched as /test/
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>