# Routes - Prevent nesting from hell?

**URL:** <https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304>\
**Category:** Questions\
**Tags:** v2\
**Created:** [February 11, 2016, 3:33pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304 "2016-02-11T15:33:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [February 11, 2016, 3:33pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/1 "2016-02-11T15:33:59Z")

</div>

**In some routes I have it looks like a nightmare:**

```php
array(
	'pattern' => '(:any)/(:any)',
	'action' => function($part1, $part2) {

```

```php
array(
	'pattern' => '(:any)/(:any)/(:any)',
	'action' => function($part1, $part2, $part3) {

```

```php
array(
	'pattern' => '(:any)/(:any)/(:any)/(:any)',
	'action' => function($part1, $part2, $part3, $part4) {

```

It’s a nightmare. A few questions to that.

- Is it possible to have one pattern that matches anything across the slashes?
- In the function, is it possible to have the arguments as one array instead of strings? The it would be easier to foreach them.

What I really want is one route that matches anything and that carries the page object.

### Image viewer

I’m building an image viewer for a site where it should do one thing if it’s an image and another thing if it’s not. It looks for a dot to determine if it’s an image or not.

```auto
my/very/long/url/with/an/image/at/the/end/my-image.png
my/very/long/url/with/an/image/at/the/end/something-else
my/image.jpg
my/something-shorter

```

I know I could create own functions to “generate” the routes, but that makes it more abstract.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 11, 2016, 3:36pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/2 "2016-02-11T15:36:06Z")

</div>

Sure. Please note that this will really match _everything_.

```php
array(
	'pattern' => '(:all)',
	'action' => function($uri) {
		$parts = explode('/', $uri);

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 11, 2016, 3:37pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/3 "2016-02-11T15:37:16Z")

</div>

Wouldn’t it make more sense then to use a regular expression? Don’t know if that get’s you the page object, though.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [February 11, 2016, 3:42pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/4 "2016-02-11T15:42:37Z")

</div>

For me it does not match anything at all. In the config file I have this:

```php
c::set('routes', array(
	array(
		'pattern' => '(:all)',
		'action' => function($uri) {
			echo $uri;
			die;
		}
	)
));

```

It loads the page as if the route was not there. No match.

I have no other routes or plugins installed except for the Patterns plugin.

### UPDATE

This worked:

```php
c::set('routes', array(
	array(
		'pattern' => '(.*)',
		'action' => function($uri) {
			echo $uri;
			die;
		}
	)
));

```

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 11, 2016, 3:50pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/5 "2016-02-11T15:50:01Z")

</div>

Oh, sorry. Seems like `(:all)` only works when there’s something else in the URL.

I replaced the pattern with `(.+)` and it worked. It will also match existing pages, so you need to handle these cases as well.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [February 11, 2016, 3:52pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/6 "2016-02-11T15:52:40Z")

</div>

Thanks! I also found a regex that worked, in the updated post above. I’m not a regex pro so I don’t know what to prefer of (.\*) or (.+)?

Thanks @texnixe for your push in the right direction.

### UPDATE

I found it myself which probably mean I should use (.+) to not match some empty string.

> <https://stackoverflow.com/questions/10763820/difference-between-and-regex>

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 11, 2016, 4:06pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/7 "2016-02-11T16:06:06Z")

</div>

Yes, `(.+)` is probably better, but you can’t normally match empty strings (a leading slash is always in the URL).

Actually the pattern `(:all)` is just a shorthand for `(.*)` in Kirby, but that doesn’t seem to work if it is alone in the pattern.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [February 11, 2016, 4:44pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/8 "2016-02-11T16:44:19Z")

</div>

Is it a bug? Add issue?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 11, 2016, 4:46pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/9 "2016-02-11T16:46:24Z")

</div>

I will look into it later and send a PR if I find the issue.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [February 11, 2016, 5:01pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/10 "2016-02-11T17:01:17Z")

</div>

It’s really strange and I haven’t found out why it is happening. See [this issue on GitHub](https://github.com/getkirby/kirby/issues/368).

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:00pm UTC](https://forum.getkirby.com/t/routes-prevent-nesting-from-hell/3304/11 "2025-02-06T17:00:17Z")

</div>


