10000 Minor changes for matchTypes by koenpunt · Pull Request #47 · dannyvankooten/AltoRouter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Minor changes for matchTypes #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions AltoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class AltoRouter {
* @param array $matchTypes
*/
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
$this->basePath = $basePath;
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
$this->setBasePath($basePath);
$this->addMatchTypes($matchTypes);

foreach( $routes as $route ) {
call_user_func_array(array($this,'map'),$route);
Expand All @@ -39,11 +39,11 @@ public function setBasePath($basePath) {
}

/**
* Add a new named match type. It uses array_merge so keys can be overwritten.
* Add named match types. It uses array_merge so keys can be overwritten.
*
* @param array $matchType The key is the name and the value is the regex.
* @param array $matchTypes The key is the name and the value is the regex.
*/
public function addMatchType($matchTypes) {
public function addMatchTypes($matchTypes) {
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
}

Expand Down
2 changes: 1 addition & 1 deletion AltoRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function testMatchWithCustomRegexp()

public function testMatchWithCustomNamedRegex()
{
$this->router->addMatchType(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
$this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route');

$this->assertEquals(array(
Expand Down
57 changes: 39 additions & 18 deletions README.md
5AEC
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,49 @@ $router->generate('users_show', array('id' => 5));

```

You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.
**You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.**

```php
* // Match all request URIs
[i] // Match an integer
[i:id] // Match an integer as 'id'
[a:action] // Match alphanumeric characters as 'action'
[h:key] // Match hexadecimal characters as 'key'
[:action] // Match anything up to the next / or end of the URI as 'action'
[create|edit:action] // Match either 'create' or 'edit' as 'action'
[*] // Catch all (lazy, stops at the next trailing slash)
[*:trailing] // Catch all as 'trailing' (lazy)
[**:trailing] // Catch all (possessive - will match the rest of the URI)
.[:format]? // Match an optional parameter 'format' - a / or . before the block is also optional

Some more complicated examples
@/(?[A-Za-z]{2}_[A-Za-z]{2})$ // custom regex, matches language codes like "en_us" etc.
/posts/[*:title][i:id] // Matches "/posts/this-is-a-title-123"
/output.[xml|json:format]? // Matches "/output", "output.xml", "output.json"
/[:controller]?/[:action]? // Matches the typical /controller/action format
* // Match all request URIs
[i] // Match an integer
[i:id] // Match an integer as 'id'
[a:action] // Match alphanumeric characters as 'action'
[h:key] // Match hexadecimal characters as 'key'
[:action] // Match anything up to the next / or end of the URI as 'action'
[create|edit:action] // Match either 'create' or 'edit' as 'action'
[*] // Catch all (lazy, stops at the next trailing slash)
[*:trailing] // Catch all as 'trailing' (lazy)
[**:trailing] // Catch all (possessive - will match the rest of the URI)
.[:format]? // Match an optional parameter 'format' - a / or . before the block is also optional
```

**Some more complicated examples**

```php
@/(?[A-Za-z]{2}_[A-Za-z]{2})$ // custom regex, matches language codes like "en_us" etc.
/posts/[*:title][i:id] // Matches "/posts/this-is-a-title-123"
/output.[xml|json:format]? // Matches "/output", "output.xml", "output.json"
/[:controller]?/[:action]? // Matches the typical /controller/action format
```

**The character before the colon (the 'match type') is a shortcut for one of the following regular expressions**

```php
'i' => '[0-9]++'
'a' => '[0-9A-Za-z]++'
'h' => '[0-9A-Fa-f]++'
'*' => '.+?'
'**' => '.++'
'' => '[^/\.]++'
```

**New match types can be added using the `addMatchTypes()` method**

```php
$router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
```


## Contributors
- [Danny van Kooten](https://github.com/dannyvankooten)
- [Koen Punt](https://github.com/koenpunt)
Expand Down
0