PHP code example of veloxrouter / router

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

    

veloxrouter / router example snippets




declare(strict_types=1);

// Load the Composer autoloader
r routes here...
$router->get('/', function ($request, $response) {
    return $response->json(['message' => 'Welcome to VeloxRouter!']);
});

// Dispatch the application
$router->run();




declare(strict_types=1);

r = new Router();

// 1. Static GET route
$router->get('/users', function ($request, $response) {
    $users = [
        ['id' => 1, 'name' => 'Ana Silva'],
        ['id' => 2, 'name' => 'Carlos Santos']
    ];

    return $response->json($users);
});

// 2. Dynamic route with path parameters and strict regex validation
$router->get('/users/{id:[0-9]+}', function ($request, $response, array $params) {
    $userId = $params['id'] ?? null;
    $Full update)
$router->put('/users/{id:[0-9]+}', function ($request, $response, array $params) {
    $userId = $params['id'] ?? null;
    $data = $request->body();

    return $response->json([
        'success' => true,
        'action' => 'PUT',
        'user_id' => $userId,
        'message' => "User {$userId} fully updated successfully!",
        'data' => $data
    ]);
});

// 5. DELETE route (Removal)
$router->delete('/users/{id:[0-9]+}', function ($request, $response, array $params) {
    $userId = $params['id'] ?? null;

    return $response->status(200)->json([
        'success' => true,
        'action' => 'DELETE',
        'user_id' => $userId,
        'message' => "User {$userId} deleted successfully!"
    ]);
});

// Start the server or dispatch the request
$router->run();