PHP code example of obscure-code / router

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

    

obscure-code / router example snippets


use ObscureCode\Router as AbstractRouter;

class Router extends AbstractRouter {
    public function patternDefault(string $path): void
    {
        $this->load('header');
        $this->load($path);
        $this->load('footer');
    }

    public function patternBlank(string $path): void
    {
        $this->load($path);
    }

    public function patternError(string $path): void
    {
        http_response_code(404);
        $this->load('header');
        $this->load('error');
        $this->load('footer');
    }
};

$config = [
    "index" => [
        "pattern" => "default",
    ],
    "ajax" => [
        "pattern" => "blank",
    ],    
    "directory" => [
        "index" => [
            "pattern" => "default",
        ],
    ],
    "error" => [
        "pattern" => "error",
    ],
];

$router = new Router(
    __DIR__ . '/

// Call />call('/');

// Call /x');

// If /, 'd', 'e'] will passed in $params
$router->call('/a/b/c/d/e');

// ['data'] will passed in $data
$router->call('/index', ['data']);



public function patternDefault(string $path): void
{
    $someClass = new SomeClass();

    $this->load('header');
    $this->load($path, ['someClass' => $someClass]);
    $this->load('footer');
}

use ObscureCode\Exceptions\NotFoundException;

try {
    ob_start();
    $router->call($route);
} catch (NotFoundException $exception) {
    ob_clean();
    $router->call('error');
} catch (SomeOtherException $exception) {
    // do something
} finally {
    ob_end_flush();
}