105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
|
|
use WillySoft\Route as App;
|
|
|
|
// generated by composer, if you are not using composer include
|
|
// the file in a different manner
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
//-----------------------------------------------------
|
|
// Basic Routing
|
|
//-----------------------------------------------------
|
|
// there are shorthands for standard HTTP request methods such as
|
|
// get|post|put|patch|delete|options
|
|
|
|
App::get('/', function() {
|
|
print('homepage');
|
|
});
|
|
|
|
// form is a shorthand that accepts GET and POST request methods
|
|
App::form('/submit',
|
|
fn() => print('i match on GET and POST methods'));
|
|
|
|
// if not satisfied you can use the match function that 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'));
|
|
|
|
// 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 request path within your route. for
|
|
// example, you may wish to capture a users id. parameters are
|
|
// injected into route callbacks based on their order.
|
|
|
|
// a required parameter will only match when a value is supplied
|
|
App::get('/echo/$text',
|
|
fn($text) => print($text));
|
|
|
|
// optional parameter; you must supply a default value
|
|
App::get('/echo_optional/$text?',
|
|
fn($text = 'You sent nothing') => print($text));
|
|
|
|
// example showing advanced multiple route parameters
|
|
App::get('/user/$user_id/friends/$pagination?', function($user_id, $pagination = '0') {
|
|
print("<h1>Showing friends for user: $user_id</h1>");
|
|
print("<p>Current page: $pagination</p>");
|
|
});
|
|
|
|
//-----------------------------------------------------
|
|
// Middlewares
|
|
//-----------------------------------------------------
|
|
// when a route is matched, it runs all middlewares first before
|
|
// entering the main route callback.
|
|
//
|
|
// 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(
|
|
fn() => print('<p>Hello all routes!</p>'));
|
|
|
|
//-----------------------------------------------------
|
|
// Route Groups
|
|
//-----------------------------------------------------
|
|
// group together routes and middlewares. a prefix can be added to
|
|
// prefix each route in the group with a given path. the group will
|
|
// be skipped if the request path does not start with the one supplied.
|
|
// middlewares added in here will only run on routes matched from
|
|
// within or any nested groups
|
|
|
|
App::group('/test', function() {
|
|
App::use(
|
|
fn() => print('<p>Hello all routes matched within this or any nested groups!</p>'));
|
|
|
|
// this will be seen as /test/
|
|
App::get('/',
|
|
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
|
|
http_response_code(404);
|
|
?>
|
|
<h1>404 not found</h1>
|
|
<p>Sorry, the page you requested could not be found</p>
|