Update example.php

This commit is contained in:
William 2025-03-26 17:20:17 +00:00
parent 49ed2107bf
commit 32c2707f7c

View File

@ -4,7 +4,6 @@ use WillySoft\Route as App;
// generated by composer, if you are not using composer include // generated by composer, if you are not using composer include
// the file in a different manner // the file in a different manner
// require __DIR__ . '/edit/this/location/file.php';
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
//----------------------------------------------------- //-----------------------------------------------------
@ -12,11 +11,9 @@ require __DIR__ . '/../vendor/autoload.php';
//----------------------------------------------------- //-----------------------------------------------------
// there are shorthands for standard HTTP request methods such as // there are shorthands for standard HTTP request methods such as
// get|post|put|patch|delete|options // get|post|put|patch|delete|options
App::get('/', function() { App::get('/', function() {
?> print('homepage');
<h1>Homepage!</h1>
<p>Welcome to the homepage</p>
<?php
}); });
// form is a shorthand that accepts GET and POST request methods // form is a shorthand that accepts GET and POST request methods
@ -28,41 +25,43 @@ App::form('/submit',
App::match('get|post|put', '/match', App::match('get|post|put', '/match',
fn() => print('i match on any method you like')); fn() => print('i match on any method you like'));
// "any" is a shorthand allowing all HTTP request methods to pass // any is a shorthand allowing all HTTP request methods to pass
App::any('/example', App::any('/example',
fn() => print('i match on any method')); fn() => print('i match on any method'));
//----------------------------------------------------- //-----------------------------------------------------
// Route Parameters // Route Parameters
//----------------------------------------------------- //-----------------------------------------------------
// capture segments of the URL_PATH within your route. for example, // capture segments of the request path within your route. for
// you may need to capture a users ID. parameters are injected into // example, you may wish to capture a users id. parameters are
// route callbacks based on their order. // injected into route callbacks based on their order.
// a required parameter will only match when a value is supplied // a required parameter will only match when a value is supplied
App::get('/echo/$text', App::get('/echo/$text',
fn($text) => print($text)); fn($text) => print($text));
// optional parameter, only the last parameter can be optional // optional parameter; you must supply a default value
// or it will throw an exception
App::get('/echo_optional/$text?', App::get('/echo_optional/$text?',
fn($text = 'You sent nothing') => print($text)); fn($text = 'You sent nothing') => print($text));
// crude friends list example // example showing advanced multiple route parameters
App::get('/user/$user_id/friends/$pagination?', function($user_id, $pagination = '0') { App::get('/user/$user_id/friends/$pagination?', function($user_id, $pagination = '0') {
?> print("<h1>Showing friends for user: $user_id</h1>");
<h1>Showing friends for user: <?=$user_id?></h1> print("<p>Current page: $pagination</p>");
<p>Current page: <?=$pagination?></p>
<?php
}); });
//----------------------------------------------------- //-----------------------------------------------------
// Middlewares // Middlewares
//----------------------------------------------------- //-----------------------------------------------------
// middlewares provide a convenient mechanism for inspecting and // when a route is matched, it runs all middlewares first before
// filtering requests. you can imagine them as a series of layers // entering the main route callback.
// that requests must pass through before they hit your application. //
// a layer can be used for auth, rate limiting or anything really // you can imagine middlewares as a series of layers that the request
// must pass through before entering your application. it can be used
// for example as authentication, rate limiting or anything really
// this middleware will run on all routes matched after it was added.
// any routes before it will not use this middleware
App::use( App::use(
fn() => print('<p>Hello all routes!</p>')); fn() => print('<p>Hello all routes!</p>'));
@ -70,17 +69,32 @@ App::use(
// Route Groups // Route Groups
//----------------------------------------------------- //-----------------------------------------------------
// group together routes and middlewares. a prefix can be added to // group together routes and middlewares. a prefix can be added to
// prefix each route in the group with a given URL_PATH. the group // prefix each route in the group with a given path. the group will
// will be skipped if the request URL_PATH does not start with the // be skipped if the request path does not start with the one supplied.
// one supplied. middlewares defined in here will only run on routes // middlewares added in here will only run on routes matched from
// matched from within or any child groups // within or any nested groups
App::group('/test', function() { App::group('/test', function() {
App::use( App::use(
fn() => print('<p>Hello all routes matched within this or any child groups!</p>')); fn() => print('<p>Hello all routes matched within this or any nested groups!</p>'));
// this will be seen as /test/ // this will be seen as /test/
App::get('/', App::get('/',
fn() => print('Testing 123')); fn() => print('Testing 123'));
// you may also define a group without a prefix. this is useful for
// seperating middlewares as middlewares defined within this group
// will not affect any routes outside of it.
App::group(callback: function() {
App::use(
fn() => print('<p>Sup G</p>'));
App::get('/nested',
fn() => print('Whats up my G?'));
});
App::get('/sup',
fn() => print('Wheres my Sup G?'));
}); });
// finally, since no route was matched, show a 404 page // finally, since no route was matched, show a 404 page