Route/example.php

65 lines
2.0 KiB
PHP

<?php
use WillySoft\Route as App;
require __DIR__ . '/../vendor/autoload.php';
// 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>'));
// 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('<p>Hello all routes matched within this or any child groups!</p>'));
// this will be matched as /test/
App::get('/',
fn() => print('Testing 123'));
// you may also define a group without a prefix
App::group('', function() {
App::get('/test',
fn() => print('Testing 456'));
});
});
// 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>