PHP code example of fastpress / router

1. Go to this page and download the library: Download fastpress/router 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/ */

    

fastpress / router example snippets


use Fastpress\Routing\Router;


$router = new Router();

// Define a simple GET route
$router->get('/hello', function() {
    echo "Hello, World!";
});

// Define a POST route with parameters
$router->post('/users/{id}', 'UserController@update');

$match = $router->match($_SERVER, $_POST);

if ($match) {
    $handler = $match['handler'];
    $params = $match['params'];
    
    // Execute the handler with the matched parameters
    // ...
} else {
    // No matching route found
    // Handle 404 Not Found
}

$router->group(['prefix' => 'admin', 'middleware' => ['auth']], function($router) {
    $router->get('/dashboard', 'AdminController@dashboard');
    $router->get('/users', 'AdminController@users');
});

$router->get('/profile/{id}', 'ProfileController@show')->name('profile.show');

// Generate URL for named route
$url = $router->getNamedRoute('profile.show', ['id' => 123]);

$router->addPattern('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');

$router->get('/users/{uuid}', 'UserController@show');

$router->get('/admin/dashboard', 'AdminController@dashboard')
    ->middleware('auth');


$router->get('/users/{id}', 'UserController@show')
    ->where(['id' => '\d+']);