vakata / router-cached
具有缓存的简单请求路由器
1.0.1
2016-10-27 11:39 UTC
Requires
- php: >=5.4.0
- vakata/cache: 1.*
- vakata/router: 1.*
Requires (Dev)
- clean/phpdoc-md: dev-master
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2024-09-10 06:06:26 UTC
README
具有缓存的简单请求路由器。
安装
通过 Composer
$ composer require vakata/router-cached
用法
// Usage is the same as with the non-cached router // you only need to supply a class implementing the \vakata\cache\CacheInterface $router = new \vakata\routerCached\RouterCached( new \vakata\cache\Filecache(__DIR__), // where will cached pages be stored 60, // cache validity in seconds - by default it is 1440 seconds function ($request, $verb) { // return a key for each request return md5($verb . ' ' . $request); // this is the default }, [ 'GET', 'POST' ], // which verbs to cache 'routeCache' // a namespace (allowing easy clearing of all cached routes) ); // everything else is exactly the same as with the non-caching router: $router ->get('/', function () { echo 'homepage'; }) ->get('/profile', function () { echo 'user profile'; }) ->with('/books/') // specify a prefix for all future routes ->get('read/{i:id}', function ($matches) { // this method uses a named placeholder // provided the user visits /books/read/10 matches will contain: var_dump($matches); // 0 => books, 1 => read, 2 => 10, id => 10 // placeholders are wrapped in curly braces {...} and can be: // - i - an integer // - a - any letter (a-z) // - h - any letter or integer // - * - anything (up to the next slash (/)) // - ** - anything (to the end of the URL) // placeholders can be named too by using the syntax: // {placeholder:name} // placeholders can also be optional // {?optional} }) // for advanced users - you can use any regex as a placeholder: ->get('{(delete|update):action}/{(\d+):id}', function ($matches) { }) // you can also use any HTTP verb ->post('delete/{i:id}', function ($matches) { }) // here is how you reset the prefix ->with('') // you can also bind multiple HTTP verbs in one go ->add(['GET', 'HEAD'], '/path', function () { }) // you can also use with() statements to execute some code if the begging of the URL is a match to the prefix ->with('user', function () { echo 1; }) ->get('view', function () { /* 1 will be echoed */ }) ->post('chat', function () { /* 1 will be echoed */ }); // there is no need to chain the method calls - this works too: $router->post('123', function () { }); $router->post('456', function () { }); // you finally run the router try { $router->run($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']); } catch (\vakata\router\RouterException $e) { // thrown if no matching route is found }
在API 文档中阅读更多
测试
$ composer test
贡献
有关详细信息,请参阅贡献指南
安全
如果您发现任何与安全相关的问题,请通过电子邮件github@vakata.com联系我们,而不是使用问题跟踪器。
致谢
许可证
MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。