Chariot 只是另一个路由库,使人性化的URL和程序员友好的代码

网友投稿 632 2022-10-31

Chariot 只是另一个路由库,使人性化的URL和程序员友好的代码

Chariot 只是另一个路由库,使人性化的URL和程序员友好的代码

Chariot

Just another routing library. Makes human-friendly URLs and programmer-friendly code. Uses trees for the best performance.

github.com/awesomite/chariot

Why?

To simplify creating human-friendly URLs.

linkTo('showArticle')->withParam('id', 5);

Table of contents

How does it work?PatternsParametersExamples RoutingGenerating linksHidden parametersCachingDefining custom patternsValidationDefault parametersTransforming parametersProviders / DecoratorsDefault patterns More examplesLicenseVersioning

How does it work?

Patterns

Patterns are designed to maximally simplify creating routing in your application. Patterns can have parameters packed in {{ double curly brackets }}.

Parameters

Parameters contains three values separated by one or more white characters. Second and third values are optional. First value is just a name. Second value is a regular expression or name of registered regular expression, default value is equal to [^/]+. Third value contains default value of parameter (used for generating links).

Examples

I believe that the best documentation are examples from the real world. The following patterns should help you to understand how does it work.

/page/{{ page :uint }}/page/{{page:uint}} (white space between first and second parameter is not required, if second parameter begins with :)/page/{{page \d+ 1}}/categories/{{ name [a-zA-Z0-9-]+ }}/categories/{{ categoryName }}/item-{{ itemId :uint }}

Routing

addRoute(HttpMethods::METHOD_GET, '/', 'home');$method = 'GET';$path = '/';try { $route = $router->match($method, $path); $handler = $route->getHandler(); echo $handler, "\n";} catch (HttpException $exception) { echo $exception->getMessage(), "\n"; // code can be equal to 404 or 405 if ($exception->getCode() === HttpException::HTTP_METHOD_NOT_ALLOWED) { echo 'Allow: ', implode(', ', $router->getAllowedMethods($path)), "\n"; }}

Generating links

addRoute(HttpMethods::METHOD_GET, '/category-{{ category :int }}', 'showCategory');echo $router->linkTo('showCategory')->withParam('category', 5), "\n";/* * Output: * /category-5 */

Hidden parameters

Hidden parameters are a very useful mechanism. Using them allows you to completely change routing in your application without changing code in a lot of places. Let's look at the following scenario:

You have a category page in your website.Path pattern to category page is equal to /category-{{ id :uint }}.Link to category page is generated in many places in your code. Let's say 100.You want to change approach. Category's id in a link is not expected anymore. You want to have human-friendly links, e.g. /books instead of /category-15.Using old school way of generating links forced you to rewrite code in 100 places. You have to spend time to rewriting code. The risk of error is high.

Instead of monotonous rewriting code you can change only one thing in routing. This approuch helps you to save your time and protects your code from bugs. The following pieces of code should help you understand how to rewrite routing.

Old code

$router->get('/category-{{ id :uint }}', 'showCategory');

New code

$router ->get('/fantasy', 'showCategory', ['id' => 1]) ->get('/comedy', 'showCategory', ['id' => 2]);

Note: In this case small number of categories (let's say 100) will not cause performance issue. But keep in your mind - big number of routes assigned to one handler can slow down generating links. I encourage you to execute performance tests on your machine. Exemplary test is attached to this repository, execute the following commands to perform it:

git clone --depth 1 git@github.com:awesomite/chariot.gitcd chariotcomposer updatephp speedtest/console.php test-links

Bigger example

get('/show-first-page', 'showPage', [ 'page' => 1,]);$router->get('/page-{{ page :uint }}', 'showPage');$route = $router->match('GET', '/show-first-page');echo $route->getHandler(), "\n";var_dump($route->getParams());/* * Output: * showPage * array(1) { * 'page' => * int(1) * } */echo $router->linkTo('showPage')->withParam('page', 1), "\n"; // /show-first-pageecho $router->linkTo('showPage')->withParam('page', 2), "\n"; // /page-2/* * Output: * /show-first-page * /page-2 */

Caching

cacheFile = $cacheFile; } public function rebuildRouter() { $router = $this->createRouter(); file_put_contents($this->cacheFile, 'exportToExecutable() . ';'); } public function getRouter(): PatternRouter { return require $this->cacheFile; } private function createRouter(): PatternRouter { return PatternRouter::createDefault() ->get('/', 'showHome') ->get('/news', 'showNews', ['page' => 1]) ->get('/news/{{ page :int }}', 'showNews'); }}$factory = new RouterFactory(__DIR__ . DIRECTORY_SEPARATOR . 'router.cache');// Executing this function once is enough, e.g. during warmup$factory->rebuildRouter();$router = $factory->getRouter();// decorators are not cacheable, you must add them each time// $router->addParamDecorator(new MyParamDecorator());

Defining custom patterns

getPatterns() ->addPattern(':date', '[0-9]{4}-[0-9]{2}-[0-9]{2}') ->addEnumPattern(':category', $categories);$router->get('/day-{{ date :date }}', 'showDay');$route = $router->match('GET', '/day-2017-01-01');echo $route->getParams()['date'], "\n"; // 2017-01-01$router->get('/category-{{ category :category }}', 'showCategory');$route = $router->match('GET', '/category-comedy');echo $route->getParams()['category'], "\n"; // comedy

Validation

Chariot checks correctness of values incoming (routing) and outcoming (generating links).

Note: Method PatternRouter->linkTo() returns instance of LinkInterface. Read description to understand difference between toString() and __toString() methods.

get('/category-{{ categoryId :int }}', 'showCategory');/* * The following code displays "Error 404" */try { $route = $router->match('GET', '/category-books'); echo "Handler:\n", $route->getHandler(), "\n"; echo "Params:\n"; var_dump($route->getParams());} catch (HttpException $exception) { echo 'Error ', $exception->getCode(), "\n";}/* * The following code displays "Cannot generate link" */try { echo $router->linkTo('showCategory')->withParam('categoryId', 'books')->toString(), "\n";} catch (CannotGenerateLinkException $exception) { echo "Cannot generate link\n";}

Default parameters

get('/articles/{{ page :uint 1 }}', 'articles');echo $router->linkTo('articles'), "\n";echo $router->linkTo('articles')->withParam('page', 2), "\n";/* * Output: * /articles/1 * /articles/2 */

Transforming parameters

Router can transform parameter extracted from URL (and parameter passed to URL). Passed object to method addPattern() must implements interface PatternInterface. @See PatternInterface.

getPatterns()->addPattern(':date', new DatePattern());$router->get('/day/{{ day :date }}', 'showDay');echo $router->linkTo('showDay')->withParam('day', new \DateTime('2017-07-07')), "\n";/* * Output: * /day/2017-07-07 */

Providers / Decorators

Let's imagine the following scenario:

You have prepared big web-application.Application is using the following pattern to display items /items/{{ id :int }}.Next goal is add title of item to url (/items/{{ id :int }}-{{ title }}).

You can just change URL pattern and add code withParam('title', $title) wherever application generates url to item. Chariot allows resolve such issues better and faster. The following code explains how to use providers. See:

ContextInterfaceParamDecoratorInterface

mapping = $mapping; } public function decorate(ContextInterface $context) { if ('showItem' !== $context->getHandler()) { return; } $id = $context->getParams()['id'] ?? null; $title = $this->mapping[$id] ?? null; if (null !== $title) { $context->setParam('title', $title); } }}$titleMapping = [ 1 => 'my-first-item', 2 => 'my-second-item', 3 => 'my-third-item',];$router = PatternRouter::createDefault();$router->get('/items/{{ id :int }}-{{ title }}', 'showItem');/* * Error, because title is not defined */echo 'Without provider: ';echo $router->linkTo('showItem')->withParam('id', 1), PHP_EOL;/* * Valid URL, because title will be provided automatically */$router->addParamDecorator(new TitleProvider($titleMapping));echo 'With provider: ';echo $router->linkTo('showItem')->withParam('id', 1), PHP_EOL;/* * Output: * * Without provider: __ERROR_CANNOT_GENERATE_LINK * With provider: /items/1-my-first-item */

Default patterns

Method Awesomite\Chariot\Pattern\Patterns::createDefault() returns instance of Awesomite\Chariot\Pattern\Patterns with set of standard patterns:

nameinputoutputclass/regex
:int-5(int) -5IntPattern
:uint5(int) 5UnsignedIntPattern
:float-5.05(float) -5.05FloatPattern
:ufloat5.05(float) 5.05UnsignedFloatPattern
:date2017-01-01new DateTimeImmutable("2017-01-01")DatePattern
:listred,green,blue(array) ["red", "green", "blue"]ListPattern
:ip48.8.8.8(string) "8.8.8.8"Ip4Pattern
:alphanumnickname2000(string) "nickname2000"[a-zA-Z0-9]+

More examples

Own micro frameworkMonthsParam decorators: example1, example2, example3Symfony integrationTransforming parameters

License

MIT - read license

Versioning

The version numbers follow the Semantic Versioning 2.0.0 scheme.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:#yyds干货盘点# leetcode算法题:最大子数组和
下一篇:Redis 内存优化神技,小内存保存大数据
相关文章

 发表评论

暂时没有评论,来抢沙发吧~