PHP code example of celiovmjr / simplerouter

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

    

celiovmjr / simplerouter example snippets




use SimpleRouter\Application\Router;

$router = new Router();

$router->get('/', function($request) {
    echo "Hello, GET request!";
});

$router->post('/post', [MyController::class, 'postMethod']);

$router->group('/api', function() use ($router) {
    // Subgroup '/v2'
    $router->subgroup('/v2', function() use ($router) {
        $router->get('/users', [UserController::class, 'getAll']);
        $router->post('/users', [UserController::class, 'create']);
    });

    // You can define more routes directly within the '/api' group
    $router->get('/status', function($request) {
        echo "API Status";
    });
});

$router->group('/admin', function() use ($router) {
    $router->get('/dashboard', function($request) {
        echo "Welcome to Admin Dashboard!";
    })->name('admin.dashboard');
});

// Resolving a named route
$route = $router->route('admin.dashboard'); // Returns '/admin/dashboard'

if ($router->dispatch()) {
    // Route found and processed successfully
} else {
    // Error handling
    $error = $router->error();
    echo "Error {$error->code}: {$error->message}";
}