PHP code example of websk / php-simple-router

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

    

websk / php-simple-router example snippets




use WebSK\SimpleRouter\SimpleRouter;

final class ArticleController
{
    public function show(string $slug): void
    {
        echo 'Article: ' . htmlspecialchars($slug, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    }
}

SimpleRouter::route(
    '~^/articles/([^/]+)$~',
    [ArticleController::class, 'show']
);

public function checkAccess(): string
{
    // Perform an access check or other intermediate processing.

    return SimpleRouter::CONTINUE_ROUTING;
}

if (SimpleRouter::matchGroup('~^/admin(?:/|$)~')) {
    SimpleRouter::route('~^/admin/users$~', [AdminUserController::class, 'list']);
    SimpleRouter::route('~^/admin/users/(\d+)$~', [AdminUserController::class, 'show']);
}

SimpleRouter::staticRoute(
    '~^/pages/([^/]+)$~',
    PageController::class,
    'show',
    null,
    __DIR__ . '/layout.php'
);

SimpleRouter::routeBasedCrud('/items', ItemController::class);

SimpleRouter::setCurrentUrlByCli('/articles/hello');
SimpleRouter::route('~^/articles/([^/]+)$~', [ArticleController::class, 'show']);

use WebSK\Config\ConfWrapper;

ConfWrapper::setConfig([
    'static_data_path' => '/var/www/example/public',
    'sitemap' => [
        'root' => '/sitemap',
    ],
    'site_domain' => 'https://example.com',
]);

use WebSK\SimpleRouter\Sitemap\InterfaceSitemapController;

final class ArticleController implements InterfaceSitemapController
{
    public function getUrlsForSitemap(): iterable
    {
        yield [
            'url' => 'https://example.com/articles/first',
            'freq' => 'weekly',
        ];

        yield [
            'url' => 'https://example.com/articles/second',
        ];
    }
}

use WebSK\SimpleRouter\SimpleRouter;
use WebSK\SimpleRouter\Sitemap\SitemapBuilder;

$builder = new SitemapBuilder();
SimpleRouter::setSitemapBuilder($builder);


SitemapBuilder::removeOldSitemapFiles();
bash
composer