PHP code example of bassta / url-manager

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

    

bassta / url-manager example snippets



Manager = new \UrlManager\UrlManager();

$urlManager->addRule(new \UrlManager\Rule('GET', '/users', 'user/list'))
           ->addRule(new \UrlManager\Rule('POST', '/users', 'user/list'))
           ->addRule(new \UrlManager\Rule('GET', '/user/{id:\d+}', 'user/view'))
           ->addRule(new \UrlManager\Rule('GET', '/articles/{id:\d+}[/{title}]', 'article/view'))
           ->addRule((new \UrlManager\Rule('GET', '/user[/{action}]', 'user/action'))->setDefault([ 'action' => 'view' ]));

$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri        = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$routeInfo  = $urlManager->parseRequest($httpMethod, $uri);

switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        // ... 404 Not Found
        break;
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed
        break;
    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        // ... call $handler with $vars
        break;
}

new \UrlManager\Rule('GET', '/user/{action}', 'user/action')
// will match '/user/view' or '/user/print' but not '/user'

(new \UrlManager\Rule('GET', '/user[/{action}]', 'user/action'))->setDefault([ 'action' => 'view' ])
// will match '/user' with $routeInfo[2]
//  array(1) {
//    'action' =>
//    string(4) "view"
//  }


Manager = new \UrlManager\UrlManager();
$urlManager->addRule(new \UrlManager\Rule('GET', '/users', 'user/list'))
           ->addRule(new \UrlManager\Rule('POST', '/users', 'user/list'))
           ->addRule(new \UrlManager\Rule('GET', '/user/{id:\d+}', 'user/view'));

$urlManager->url('user/list');
// returns '/users'

$urlManager->url('user/view', [ 'id' => 1 ]);
// returns '/user/1'

$urlManager->url('user/view', [ 'id' => 1, 'foo' => 'bar' ]);
// returns '/user/1?foo=bar'

$urlManager->url('user/view', [ 'id' => 1, 'foo' => 'bar', '#' => 'fragment' ]);
// returns '/user/1?foo=bar#fragment'