PHP code example of magnus / router

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

    

magnus / router example snippets




$router = new \Magnus\Router\Object\ObjectRouter();


namespace Magnus\Request;

class Context {
    protected $requestURI;
    protected $baseURI;
    protected $requestPath;
    protected $appMode;
    protected $logger;
    protected $controllerPrefix;

    public function __construct(Array $config) {
        $this->documentRoot     = isset($config['documentRoot']) 
                                ? $this->normalizeURI($config['documentRoot']) 
                                : $this->normalizeURI($_SERVER['DOCUMENT_ROOT']);

        $this->requestURI       = isset($config['requestURI']) 
                                ? $this->normalizeURI($config['requestURI'])
                                : '/';

        $this->baseURI          = isset($config['baseURI']) 
                                ? str_replace($this->documentRoot, '', $this->normalizeURI($config['baseURI']))
                                : '/';

        $this->assetRoot        = isset($config['assetRoot'])
                                ? $this->normalizeURI($config['assetRoot'])
                                : rtrim($this->baseURI, '/') . '/src/assets';

        $this->appMode          = isset($config['appMode']) 
                                ? $config['appMode'] 
                                : 'DEVELOPMENT';

        $this->logger           = isset($config['logger']) 
                                ? $config['logger'] 
                                : null;

        $this->controllerPrefix = isset($config['controllerPrefix']) 
                                ? $config['controllerPrefix'] 
                                : 'Magnus\\Controllers\\';
        
        $this->requestPath = explode('/', $this->requestURI);
        
        if (end($this->requestPath) === '') {
            array_pop($this->requestPath);
        }

        if ($this->requestPath[0] === '') {
            array_shift($this->requestPath);
        }

    }

    public function normalizeURI($uri) {
        return strtolower(str_replace('\\', '/', $uri));
    }

    public function getRequestURI() {
        return $this->requestURI;
    }

    public function getAssetRoot() {
        return $this->assetRoot;
    }

    public function setRequestURI($uri) {
        $this->requestURI = $this->normalizeURI($uri);
    }

    public function getBaseURI() {
        return $this->baseURI;
    }
    public function setBaseURI($uri) {
        $this->baseURI = $this->normalizeURI($uri);
    }

    public function getRequestPath() {
        return $this->requestPath;
    }

    public function getAppMode() {
        return $this->appMode;
    }

    public function getLogger() {
        return $this->logger;
    }

    public function getControllerPrefix() {
        return $this->controllerPrefix;
    }

}

foreach ($router($context, $root) as $signal) {
    list($object, $chunk, $path, $isEndpoint) = $signal;
    if ($isEndpoint) {
        //Your chosen handling process for obtaining the server response data from $object
    }
}

namespace Magnus\Controllers;
class RootController {

    public function __invoke($args = []) {
        return [];
    }
}

namespace Magnus\Controllers;
class RootController {

    public $user;

    public function __construct($context) {
        $this->user = new UsersController();
    }

    public function __invoke($args = []) {
        return [];
    }
}

namespace Magnus\Controllers;
class UsersController {

    public function __invoke($args = []) {
        return [];
    }

    public function lookup($path, $context) {
        $userID = $path[0];
        $userProfile = new UserController($userID);
        return [$userProfile, [$path[0]]];
    }
}