# Get a route pattern/regex?

**URL:** <https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755>\
**Category:** Questions\
**Created:** [August 17, 2017, 9:29pm UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755 "2017-08-17T21:29:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SVL](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SVL](https://discourse.slimframework.com/u/SVL)\
**Post date:** [August 17, 2017, 9:29pm UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755/1 "2017-08-17T21:29:18Z")

</div>

Considering this route:  
`$app->map(['GET', 'POST'], '/step[/{step:[1-5]+}]', App\Controller\StepController::class)->setName('step');`

How can my controller/middleware get access to used pattern?  
Basicly I need this part to work with it further:

> ‘/step[/{step:[1-5]+}]’

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [August 18, 2017, 1:03am UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755/2 "2017-08-18T01:03:20Z")

</div>

```php
$args['step'];

```

Does the [Regular Expression Matching](https://www.slimframework.com/docs/objects/router.html#regular-expression-matching) section in the Router docs help?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [August 18, 2017, 1:07am UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755/3 "2017-08-18T01:07:32Z")

</div>

Or, in another example:

> <https://github.com/akrabat/slim-bookshelf/blob/master/app/src/routes.php#L7>

> <https://github.com/akrabat/slim-bookshelf/blob/master/app/src/Bookshelf/AuthorController.php#L30>

> <https://github.com/akrabat/slim-bookshelf/blob/master/app/src/Bookshelf/AuthorController.php#L32>

---

<div class="post-metadata">

**Author:** ![SVL](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SVL](https://discourse.slimframework.com/u/SVL)\
**Post date:** [August 18, 2017, 6:43am UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755/4 "2017-08-18T06:43:47Z")

</div>

Perhaps I explained poorly.  
What I need is literally ‘/step[/{step:[1-5]+}]’.

**$route** contains the pattern but it is protected:  
`$route = $request->getAttribute('route');`  
`[pattern:protected] => /step[/{step:[1-5]+}]`

Is there a way to get that into my controller or middleware?

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [August 23, 2017, 5:41pm UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755/5 "2017-08-23T17:41:49Z")

</div>

You can extend the `Slim\Route` class and provide a public getter for pattern. Since the route is created in `Slim\Router` class and there is no factory class you can pass in, you need to extend `Slim\Router` as well. For example:

```php
<?php

require_once __DIR__. '/vendor/autoload.php';

// extend Route to provide public getter for pattern
class MyRoute extends Slim\Route
{
    public function getPattern() {
        return $this->pattern;
    }
}

// extend router to use the extended Route class
class MyRouter extends Slim\Router
{
    protected function createRoute($methods, $pattern, $callable)
    {
        $route = new MyRoute($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter);
        if (!empty($this->container)) {
            $route->setContainer($this->container);
        }

        return $route;
    }
}

// Set the router in the container, so Slim uses the extended Router class instance
$app = new Slim\App(['router' => new MyRouter()]);

$app->get('/step[/{step:[1-5]+}]', function (Slim\Http\Request $request, Slim\Http\Response $response, array $params) {
    $route = $request->getAttribute('route');

    return $route->getPattern();
});

$app->run();

```

Another way would be to pass in the pattern as an argument for the controller. You’d have to specify the pattern twice, but you won’t need to extend classes:

```php
$app->get('/step[/{step:[1-5]+}]', function (Slim\Http\Request $request, Slim\Http\Response $response, array $params) {
    $controller = new SomeController('/step[/{step:[1-5]+}]'); // pass pattern by constructor

    return $controller->someAction($request, $response, $params);
});

```

A more hackish way, that I don’t particularly recommend, would be to use [ReflectionProperty](http://php.net/reflectionproperty) to gain access to the property:

```php
    $property = new \ReflectionProperty(get_class($route), 'pattern');
    $property->setAccessible(true);
    return $property->getValue($route);

```

Also, you can create a pull request that adds a public getter for the pattern 😉 Once accepted, there is no needed for jumping through hoops to get this variable.

Hope that helps,

Lennaert

---

<div class="post-metadata">

**Author:** ![SVL](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SVL](https://discourse.slimframework.com/u/SVL)\
**Post date:** [August 25, 2017, 7:58am UTC](https://discourse.slimframework.com/t/get-a-route-pattern-regex/1755/6 "2017-08-25T07:58:31Z")

</div>

Thanks very much, that is great code that even works and returns the pattern.  
I probally will ask for a getPattern public function, there are usercases where one needs just the pattern.
