PHP code example of pagemill / router

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

    

pagemill / router example snippets


$r = new PageMill\Router\Router(
    [
        [
            "type" => "exact",
            "pattern" => "/foo",
            "action" => "Foo"
        ],
        [
            "type" => "exact",
            "pattern" => "/foo/bar",
            "action" => "FooBar"
        ],
    ]
);

$route = $r->match("/foo");

$r = new PageMill\Router\Router();
$r->add("exact", "/foo", "Foo");
$r->add("exact", "/foo/bar/", "FooBar");
$route = $r->match("/foo");

$r = new PageMill\Router\Router(
    [
        [
            "type" => "starts_with",
            "pattern" => "/foo/",
            "action" => "Foo",
            "tokens" = [
                "group",
                "id"
            ]
        ]
    ]
);

$route = $r->match("/foo/1/2/");

array(
    "type" => "starts_with",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
)

$r = new PageMill\Router\Router(
    [
        [
            "type" => "regex",
            "pattern" => "/foo/(\d+)/(\d+)/",
            "action" => "Foo",
            "tokens" = [
                "group",
                "id"
            ]
        ]
    ]
);

$route = $r->match("/foo/1/2/");

[
    "type" => "regex",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "regex",
            "pattern" => "/foo/(\d+)/(\d+)/",
            "action" => "Foo",
            "host" => "www.example.com",
            "method" => "GET"
        ]
    ]
);
$route = $r->match("/foo/1/2/");

[
    "type" => "starts_with",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
    "host" => "www.example.com",
    "method" => "GET"
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "regex",
            "pattern" => "/foo/(\d+)/(\d+)/",
            "action" => "Foo",
            "host" => [
                "type" => "regex",
                "pattern" => '/\.example\.com$/'
            ],
            "method" => "GET"
        ]
    ]
);
$route = $r->match("/foo/1/2/");

[
    "type" => "starts_with",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
    "host" => "www.example.com",
    "method" => "GET"
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "regex",
            "pattern" => "/foo/(\d+)/(\d+)/",
            "action" => "Foo",
            "accept" => [
                "text/html"
            ]
        ]
    ]
);
$route = $r->match("/foo/1/2/");

[
    "type" => "starts_with",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
    "accept" => "text/html"
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "regex",
            "pattern" => "/foo/(\d+)/(\d+)/",
            "action" => "Foo",
            "accept" => [
                "application/json"
                "text/html"
            ]
        ]
    ]
);
$route = $r->match("/foo/1/2/");

[
    "type" => "starts_with",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
    "accept" => "text/html"
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "regex",
            "pattern" => "/foo/(\d+)/(\d+)/",
            "action" => "Foo",
            "headers" => [
                "Authorization" => [
                    "type" => "exact",
                    "pattern" => '12345678'
                ]
            ]
        ]
    ]
);
$route = $r->match("/foo/1/2/");

[
    "type" => "starts_with",
    "pattern" => "/foo/",
    "action" => "Foo",
    "tokens" = [
        "group" => 1,
        "id" => 2
    ]
    "headers" => [
        "Authorization" => "12345678"
    ]
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "exact",
            "pattern" => "/foo/",
            "action" => "Foo",
        ],
        [
            "type" => "default",
            "action" => "Default",
        ]
    ]
);
$route = $r->match("/foo/1/2/");

[
    "type" => "default",
    "action" => "Default",
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "starts_with",
            "pattern" => "/foo",
            "routes" => [
                [
                    "type" => "exact",
                    "pattern" => "/foo/bar",
                    "action" => "FooBar"
                ],
                [
                    "type" => "exact",
                    "pattern" => "/foo/baz",
                    "action" => "FooBaz"
                ],
            ]
        ]
    ]
);
$route = $r->match("/foo/bar");

[
    "type" => "exact",
    "pattern" => "/foo/bar",
    "action" => "FooBar"
]

$r = new PageMill\Router\Router(
    [
        [
            "type" => "exact",
            "pattern" => "/foo/",
            "action" => "Foo",
        ],
        [
            "type" => "default",
            "action" => "Default",
        ]
    ]
);
$routes = $r->get_routes();