# The official way to get the route pattern?

**URL:** <https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 11, 2017, 1:40pm UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247 "2017-12-11T13:40:41Z")\
**Posts on this page:** 6\
**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:** [December 11, 2017, 1:40pm UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247/1 "2017-12-11T13:40:41Z")

</div>

Sometimes I need to get the matched pattern from a route.

**It can be done like this:**

```php
kirby()->routes([
    [
        'pattern' => ['jens', 'erik'],
        'action' => function() {
            echo $this->kirby->request()->path()->first(); // jens
        }
    ],
]);

```

Is this the official way to do it? I think it’s quite long. I would prefer to have it like:

```php
echo $this->pattern();

```

But it looks like with `$this` I get the full kirby object so `$this->pattern` would not make sense there.

---

<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:** [December 11, 2017, 5:46pm UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247/2 "2017-12-11T17:46:20Z")

</div>

Don’t know what your are doing with the `$this` variable, but this should work:

```auto
kirby()->routes([
    [
        'pattern' => ['jens', 'erik'],
        'action' => function() {
          echo kirby()->route()->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:** [December 12, 2017, 6:47am UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247/3 "2017-12-12T06:47:12Z")

</div>

Ahh, that’s better. I also found out that it’s possible to use `$this->kirby->route()->pattern()` but it’s longer.

**My full route now looks like this:**

```php
<?php
global $kirby;

foreach(page('static')->children() as $item) {
    $pattern[] = $item->slug();
}

$kirby->routes([
    [
        'pattern' => $pattern,
        'action' => function() {
            $uid = 'static' . '/' . kirby()->route()->pattern();
            return site()->visit($uid);
        }
    ],
]);

```

---

<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:** [December 21, 2017, 7:44pm UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247/4 "2017-12-21T19:44:15Z")

</div>

What about this?

```auto
$slugs = page('static')->children()->pluck('slug');

$kirby->routes([
    [
        'pattern' => '(' . implode('|', $slugs) . ')',
        'action' => function($slug) {
            $uid = 'static' . '/' . $slug;
            return site()->visit($uid);
        }
    ],
]);

```

That’s probably the more official way to do this.

But apart from that: I have noted the idea to provide the route object as `$this` for Kirby 3. We will still need to check if that even makes sense though (maybe it breaks another use-case).

---

<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:** [December 21, 2017, 8:01pm UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247/5 "2017-12-21T20:01:50Z")

</div>

Yeah, that’s a nice way of solving this, didn’t think of converting the array to a placeholder…

---

<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:30pm UTC](https://forum.getkirby.com/t/the-official-way-to-get-the-route-pattern/9247/6 "2025-02-06T17:30:18Z")

</div>


