PHP code example of pivotphp / core-routing

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

    

pivotphp / core-routing example snippets


use PivotPHP\Routing\Router;

// Create router instance
$router = new Router();

// Define routes
$router->get('/users', function($req, $res) {
    return $res->json(['users' => []]);
});

$router->post('/users', [UserController::class, 'store']);

// Route with parameters
$router->get('/users/:id', function($req, $res) {
    $userId = $req->param('id');
    return $res->json(['user' => ['id' => $userId]]);
});

// Route groups with prefix and middleware
$router->group('/api', function() use ($router) {
    $router->get('/status', function($req, $res) {
        return $res->json(['status' => 'ok']);
    });
}, $authMiddleware);

// Match route
$route = $router::identify($method, $path);

use PivotPHP\Routing\Plugins\MetricsPlugin;

$router->registerPlugin(new MetricsPlugin());

use PivotPHP\Routing\Cache\FileCacheStrategy;

$cache = new FileCacheStrategy('/path/to/cache');
$router->setCacheStrategy($cache);

// Warm cache
$router->warmCache();

use PivotPHP\Routing\Static\StaticFileManager;

$router->static('/public', __DIR__ . '/public');