Download the PHP package awesomite/chariot without Composer
On this page you can find all versions of the php package awesomite/chariot. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package chariot
Chariot
Just another routing library. Makes human-friendly URLs and programmer-friendly code. Uses trees for the best performance.
Why?
To simplify creating human-friendly URLs.
Table of contents
- How does it work?
- Patterns
- Parameters
- Examples
- Routing
- Generating links
- Hidden parameters
- Caching
- Defining custom patterns
- Validation
- Default parameters
- Transforming parameters
- Providers / Decorators
- Default patterns
- More examples
- License
- Versioning
- Patterns
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
Generating links
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
New code
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:
Bigger example
Caching
Defining custom patterns
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.
Default parameters
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.
Providers / Decorators
Let's imagine the following scenario:
1) You have prepared big web-application.
2) Application is using the following pattern to display items /items/{{ id :int }}
.
3) 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:
- ContextInterface
- ParamDecoratorInterface
Default patterns
Method Awesomite\Chariot\Pattern\Patterns::createDefault()
returns instance of Awesomite\Chariot\Pattern\Patterns
with set of standard patterns:
name | input | output | class/regex |
---|---|---|---|
:int | -5 |
(int) -5 |
IntPattern |
:uint | 5 |
(int) 5 |
UnsignedIntPattern |
:float | -5.05 |
(float) -5.05 |
FloatPattern |
:ufloat | 5.05 |
(float) 5.05 |
UnsignedFloatPattern |
:date | 2017-01-01 |
new DateTimeImmutable("2017-01-01") |
DatePattern |
:list | red,green,blue |
(array) ["red", "green", "blue"] |
ListPattern |
:ip4 | 8.8.8.8 |
(string) "8.8.8.8" |
Ip4Pattern |
:alphanum | nickname2000 |
(string) "nickname2000" |
[a-zA-Z0-9]+ |
More examples
- Own micro framework
- Months
- Param decorators: example2, example3
- Symfony integration
- Transforming parameters
License
MIT - read license
Versioning
The version numbers follow the Semantic Versioning 2.0.0 scheme.
All versions of chariot with dependencies
ext-pcre Version *