PHP code example of kilahm / attribute-router

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

    

kilahm / attribute-router example snippets


    <<route('get', '/a')>>
    public static function getA(Container $c, Vector<string> $matches) : void
    {
        ...
    }

// bootstrap
$container = new Container();
$router = new kilahm\AttributeRouter\Router(new AutoRoutes($container));
$app = new App($container, $router); // Pass the router and container to your main application class
$app->run(); // Or however your application class works...

class Container
{
    public function makeRouter() : \kilahm\AttributeRouter\Router
    {
        return new \kilahm\AttributeRouter\Router(new AutoRoutes($this));
    }
}

$path = $_SERVER['REQUEST_URI']; // Or any other way to get the path to match
// Somehow determine which HTTP verb was used to access this resource
$router->match($path, HttpVerb::Get); // Or the appropriate verb

    <<route('/pattern/(.*)/(.*)')>>
    public static function patternA(Container $c, Vector<string> $matches) : void
    {
        // If the original path was /pattern/foo/bar, then
        // $matches[0] is ‘/pattern/foo/bar’
        // $matches[1] is ‘foo’
        // $matches[2] is ‘bar’
    }

    <<route(‘delete’, ‘/user’)>>
    public static function deleteUser(Container $c, Vector<string> $matches) : void
    {
        // $matches == Vector{‘/user’}
    }