redcatphp/route

此包已被废弃且不再维护。未建议替代包。

路由 - 用于管理应用程序入口点的微框架

v1.8.1 2018-04-03 12:00 UTC

This package is not auto-updated.

Last update: 2020-01-26 00:49:13 UTC


README

不再积极维护。我现在使用 NodeJS,并推荐您查看 express

路由您的应用程序请求

Route 是一个用于灵活简单管理请求的 微框架

FrontController

这是一个基本的 front-controller 类,您可以扩展它来构建应用程序的前端映射。它使用一个路由器来处理请求。实现了 ArrayAccess 接口和 runFromGlobals 方法,这些方法用于通用 RedCat 的 index.php
扩展

class MyFrontOffice extends \\RedCat\\Route\\FrontOffice{  
    function \_\_construct(Router $router,Di $di){  
        parent::\_\_construct($router,$di);  
        $this->map([  
            ['backend/','new:RedCat\\Plugin\\FrontController\\Backoffice'],  
            [['new:RedCat\\Route\\Match\\Extension','css|js|png|jpg|jpeg|gif'],'new:RedCat\\Plugin\\FrontController\\Synaptic'],  
            [['new:RedCat\\Plugin\\RouteMatch\\ByTml'.($this->l10n?'L10n':''),'','template'],'new:RedCat\\Plugin\\Templix\\Templix'.($this->l10n?'L10n':'')],  
        ]);  
    }  
    function run($path,$domain=null){  
        if(!parent::run($path,$domain)){  
            http\_response\_code(404);  
            print "Page not found !";  
            exit;  
        }  
        return true;  
    }  
}  
            

然后,使用它

use RedCat\\Route\\Match\\Prefix;  
use RedCat\\Route\\Match\\Suffix;  
use RedCat\\Route\\Match\\Regex;  
use RedCat\\Route\\Match\\Extension;  
  
$f = new MyFrontOffice();  
            

追加和预追加方法

$this->append(new Prefix('test/'),function($path){  
    print "My url start with 'test' followed by '$path'";  
});  
$this->prepend(new Prefix('test/more'),function($path){  
    print "My url start with 'test/more' followed by '$path'";  
});  
$f->append(new Suffix('.stuff'),function($path){  
    print "My url end with '.stuff' preceded by '$path'";  
});  
            

在第三个参数中使用类似 Z-index 的 API(默认为零)
它将首先查找匹配 ".stuff" 的,然后是 "test/more",最后是 "test/"

$this->append(new Prefix('test/'),function($path){  
    print "My url start with 'test' followed by '$path'";  
},2);  
$this->prepend(new Prefix('test/more'),function($path){  
    print "My url start with 'test/more' followed by '$path'";  
},2);  
$f->append(new Suffix('.stuff'),function($path){  
    print "My url end with '.stuff' preceded by '$path'";  
},1);  
            

参数自动包装

// test/more is a string, consequently it will be wrapped automaticaly by Prefix object  
$this->prepend('test/more',function($path){  
    print "My url start with 'test/more' followed by '$path'";  
});  
  
// string start with "/^" and end with "$/", consequently it will be wrapped automaticaly by Regex object  
$this->append('/^blog/(\\w+)/(\\d+)$/',function($category$id){  
    // if url is blog/php/200 it will print "php:200"  
    print $category.':'.$id;  
});  
            

空 URL 处理

$f->append('',function(){  
    print 'You are on home page !';  
});  
            

延迟加载 match,数组中的第一个元素以 "new:" 开头,对象仅在必要时(之前的匹配未成功)才被实例化

$f->append(['new:RedCat\\Route\\Match\\Suffix','.stuff'],function($path){  
    print "My url end with '.stuff' preceded by '$path'";  
});  
            

延迟加载 callback,数组中的第一个元素或字符串以 "new:" 开头,对象仅在必要时(匹配成功)才被实例化

// Class instanciation and method  
$f->append('hello',[['new:MyModuleClass'],'methodName']);  
  
// Class instanciation with construct params and method  
$f->append('hello',[['new:MyModuleClass',$param1ForInstanciation,$param2ForInstanciation],'methodName']);  
  
// Class instanciation and invokation  
//   object will be invoked after instanciation using \_\_invoke magic method if exists  
$f->append('hello','new:MyModuleClass');  
            

运行

//manual url  
$f->run('test/');  
  
//automatic current url  
$f->runFromGlobals();  
            

路由器

路由器是 FrontController 用于映射、追加和预追加匹配到行为的组件。它支持在 FrontController 中解释过的先前方法,但除了 runFromGlobals

匹配

基本匹配组件分布在 RedCat\Route\Match 命名空间下,但 RedCat\Plugin\RouteMatch 命名空间下也有一些特定匹配的示例。创建 Match 对象的唯一规则是它必须是可以调用的,实现 __invoke 魔法方法。您还可以使用 PHP Closure,也称为 匿名函数

URL

URL 是一个用于从 URL 提取一些简单组件的小助手。

$url = new Url;  
  
\# http:// or https://  
$url->getProtocolHref();  
  
\# mydomain.com  
$url->getServerHref();  
  
\# output integer number of port if different from default (80 for http and 443 for https)  
$url->getPortHref();  
  
\# root-path-of-redcat/  
$url->getSuffixHref();  
  
\# http://mydomain.com/root-path-of-redcat/  
$url->getBaseHref();  
  
\# http://mydomain.com/root-path-of-redcat/current-path/  
$url->getLocation();  
  
\# http://mydomain.com/root-path-of-redcat/  
$url->getSubdomainHref();  
  
\# http://fr.mydomain.com/root-path-of-redcat/  
$url->getSubdomainHref('fr');  
  
\# if current subdomain contain 2 character it will output them  
$url->getSubdomainLang();