PHP code example of benson / route-me

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

    

benson / route-me example snippets


     

    $router = new Router();
    

    $router->get('/users', function () {
        // Handle GET request to /users
    });

    // Add class method as callback
    $router->addRoute('GET', '/users', "App\Controllers\UserController@index");

    // Add route parameters
    $router->addRoute('GET', '/users/{id}', function ($id) {
        // Handle GET request to /users/{id}
    });

    // Add middleware
    $router->addRoute(
        'GET', // HTTP method
        '/user/{id}', // URL pattern
        'App\Controllers\UserController@show', // Callback function
        'App\Middleware\Authenticate@handle' // Middleware function
        );

        // Add middleware to all routes
        $router->withMiddleware('App\Middleware\Authenticate@handle');

    $router->post('/users', function () {
        // Handle POST request to /users
    });
    

    $router->run();