PHP code example of qingbing / php-url-manager

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

    

qingbing / php-url-manager example snippets



// 获取 urlManager 实例
$urlManager = UrlManager::getInstance('url-manager');

// 换取 urlRule 下的 pathinfo
$pathinfo = $urlManager->parseUrl();
var_dump($pathinfo);
var_dump($_GET);
var_dump('===================');


return [
    'routeVar' => "r", // get 模式下路由的标志
    'urlSuffix' => ".shtml", // path 模式下链接的后缀名
    'showScriptName' => true, // path 创建URL访问链接时是否显示脚本名
    'appendParams' => true, // 参数是否增加在 pathInfo 后
    'cacheInstanceString' => "\Components\FileCache::getInstance('cache-file');", // cache 实例的实例化字符串代码,设置为空表示不缓存规则
    'caseSensitive' => "false", // 链接的地址是否区分大小写
    'urlFormat' => "path", // 路由的显示模式,get和path两种
    'throwNotMatchRule' => true, // 当无匹配的url规则时是否抛出异常
    'rules' => [
        // 两级路由
        [
            'pattern' => '',
            'route' => 'site/index',
//            'defaultParams' => ['area' => 'chengdu'],
        ], [
            'pattern' => '<controller:\w+>',
            'route' => '<controller>/index',
        ], [
            'pattern' => '<controller:\w+>/add/*',
            'route' => '<controller>/add',
        ], [
            'pattern' => '<controller:\w+>/<action:(edit|delete)>/<id:\d+>/*',
            'route' => '<controller>/<action>',
        ], [
            'pattern' => '<controller:\w+>/<id:\d+>/*',
            'route' => '<controller>/view',
        ], [
            'pattern' => '<controller:\w+>/list/*',
            'route' => '<controller>/index',
        ], [
            'pattern' => '<controller:\w+>/<action:\w+>/*',
            'route' => '<controller>/<action>',
        ],

        // 三级路由
        /*[
            'pattern' => '',
            'route' => 'home/default/index',
        ], [
            'pattern' => '<module:\w+>',
            'route' => '<module>/default/index',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>',
            'route' => '<module>/<controller>/index',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/add/*',
            'route' => '<module>/<controller>/add',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/<action:(edit|delete)>/<id:\d+>/*',
            'route' => '<module>/<controller>/<action>',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/<id:\d+>/*',
            'route' => '<module>/<controller>/view',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/list/*',
            'route' => '<module>/<controller>/index',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/<action:\w+>/*',
            'route' => '<module>/<controller>/<action>',
        ],*/
    ],
];


// 创建 URL
$url = $urlManager->createUrl('home/default/index');
var_dump($url);

$url = $urlManager->createUrl('admin/default/index');
var_dump($url);

$url = $urlManager->createUrl('admin/good/add');
var_dump($url);

$url = $urlManager->createUrl('admin/good/add', ['id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('admin/good/delete', ['sex' => 'nv', 'id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('admin/good/edit', ['id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('admin/good/view', ['id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('admin/good/index');
var_dump($url);

$url = $urlManager->createUrl('admin/good/index', ['id' => 5, 'sex' => 'nv']);
var_dump($url);

$url = $urlManager->createUrl('admin/good/list');
var_dump($url);

$url = $urlManager->createUrl('admin/good/list', ['id' => 5, 'sex' => 'nan']);
var_dump($url);

$url = $urlManager->createUrl('admin/site/test', ['id' => 5]);
var_dump($url);


; URL 的规则 - 二级路由(<controller>/<action>)
    'rules' => [
        // 两级路由
        [
            'pattern' => '',
            'route' => 'site/index',
//            'defaultParams' => ['area' => 'chengdu'],
        ], [
            'pattern' => '<controller:\w+>',
            'route' => '<controller>/index',
        ], [
            'pattern' => '<controller:\w+>/add/*',
            'route' => '<controller>/add',
        ], [
            'pattern' => '<controller:\w+>/<action:(edit|delete)>/<id:\d+>/*',
            'route' => '<controller>/<action>',
        ], [
            'pattern' => '<controller:\w+>/<id:\d+>/*',
            'route' => '<controller>/view',
        ], [
            'pattern' => '<controller:\w+>/list/*',
            'route' => '<controller>/index',
        ], [
            'pattern' => '<controller:\w+>/<action:\w+>/*',
            'route' => '<controller>/<action>',
        ],

        // 三级路由
        /*[
            'pattern' => '',
            'route' => 'home/default/index',
        ], [
            'pattern' => '<module:\w+>',
            'route' => '<module>/default/index',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>',
            'route' => '<module>/<controller>/index',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/add/*',
            'route' => '<module>/<controller>/add',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/<action:(edit|delete)>/<id:\d+>/*',
            'route' => '<module>/<controller>/<action>',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/<id:\d+>/*',
            'route' => '<module>/<controller>/view',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/list/*',
            'route' => '<module>/<controller>/index',
        ], [
            'pattern' => '<module:\w+>/<controller:\w+>/<action:\w+>/*',
            'route' => '<module>/<controller>/<action>',
        ],*/
    ],



// 二级创建 URL
$url = $urlManager->createUrl('site/index');
var_dump($url);

$url = $urlManager->createUrl('good/add');
var_dump($url);

$url = $urlManager->createUrl('good/add', ['id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('good/delete', ['sex' => 'nv', 'id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('good/edit', ['id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('good/view', ['id' => 22]);
var_dump($url);

$url = $urlManager->createUrl('good/index');
var_dump($url);

$url = $urlManager->createUrl('good/index', ['id' => 5, 'sex' => 'nv']);
var_dump($url);

$url = $urlManager->createUrl('good/list');
var_dump($url);

$url = $urlManager->createUrl('good/list', ['id' => 5, 'sex' => 'nan']);
var_dump($url);

$url = $urlManager->createUrl('site/test', ['id' => 5]);
var_dump($url);