1. Go to this page and download the library: Download sformisano/jetrouter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sformisano / jetrouter example snippets
// Import the JetRouter
use JetRouter\Router;
// Router config
$config = [];
// Create the router instance
$r = Router::create($config);
$r->addRoute('GET', 'some/resource/path', 'the_route_name', function(){
// the callback fired when a request matches this route
});
$r->get(‘users/new’, ‘route_name’, function(){
// new user form view
});
$r->post('users', 'create_user', function(){
// create user in database
});
$r->get('users/{username}', 'get_user_by_username', function($username){
echo "Welcome back, $username!";
});
$->get('schools/{school}/students/{year}', 'get_school_students_by_year', function($school, $year){
echo "Welcome, $school students of $year!";
});
/**
* One or more characters that is not a '/'
*/
const DEFAULT_PARAM_VALUE_REGEX = '[^/]+';
$r->get('schools/{school}/students/{year:[0-9]+}', 'get_school_students_by_year', function($school, $year){
// If $year is not an integer an exception will be thrown
});
$r->get('schools/{school}/students/{year:i}', 'get_school_students_by_year', function($school, $year){
// If $year is not an integer an exception will be thrown
});
$r->get('schools/{school}/students/{year}?’, 'get_school_students_by_year', function($school, $year){
// $year can be empty!
});
$r->get('files/{filter_name}?/{filter_value}?/latest', 'get_latest_files', function($filter_name, $filter_value){
// get files and work with filters if they have a value
});
// endpoint doing stuff up here
return [ 'respond_to' => [
'json' => $output // this is whatever came out of this endpoint, to be returned as json!,
'html' => function(){
// this callback will run if this the route handler is dispatching a standard, synchronous request
// do something here, then maybe redirect with wp_redirect or whatever else!
}
] ];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.