# Routing paths aliases

**URL:** <https://discourse.slimframework.com/t/routing-paths-aliases/4160>\
**Category:** Questions\
**Created:** [May 14, 2020, 2:12pm UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160 "2020-05-14T14:12:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [May 14, 2020, 2:12pm UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/1 "2020-05-14T14:12:18Z")

</div>

Here is the problem.  
I got two route paths to use for the same source - first frontend side - SEO friendly “/paper\_bag\_laminate”, second backend side for internal dynamic redirections “/articles/{item}/{feature}”. Does anybody know any elegant way to declare one as an alias of another to be able to trigger the same source without a need to re-declare it?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 14, 2020, 2:24pm UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/2 "2020-05-14T14:24:56Z")

</div>

You could invoke a single action controller (instead of closures).

---

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [May 14, 2020, 2:30pm UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/3 "2020-05-14T14:30:29Z")

</div>

I was counting on you to be quite frank :). Even if, how can I avoid than double call of the same action within two different paths declaration ?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 14, 2020, 2:40pm UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/4 "2020-05-14T14:40:29Z")

</div>

I’m not sure what you mean. Normally only one action (route) is executed per HTTP request.

---

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [May 14, 2020, 3:06pm UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/5 "2020-05-14T15:06:20Z")

</div>

Yes but as I said, I wish to have a possibility to call the same action by two different routes without a need to repeat the same callback body

```php
    $app->map(['GET', 'POST'], '/paper_bags_laminate', function (Request $request, Response $response) {
        return $this->get(ArticleController::class)->add($request, $response, 1, 'paperbag');
    })->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

    $app->map(['GET', 'POST'], '/article/{item}/{feature}', function (Request $request, Response $response, array $args) {
        return $this->get(ArticleController::class)->add($request, $response, $args['item'], $args['feature']);
    })->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

```

---

<div class="post-metadata">

**Author:** ![maurobonfietti](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/maurobonfietti/32/1114_2.png) [@maurobonfietti](https://discourse.slimframework.com/u/maurobonfietti)\
**Post date:** [May 15, 2020, 2:09am UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/6 "2020-05-15T02:09:03Z")

</div>

The **map** method it’s interesting to handles multiple HTTP request methods. However, I never used it (yet).

Maybe you can do something like this:

_(I do not tested the code, it’s just an idea)_

- In your routes:

```php
    $app->map(['GET', 'POST'], '/paper_bags_laminate', 'ArticleController:add')
        ->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

    $app->map(['GET', 'POST'], '/article/{item}/{feature}', 'ArticleController:add')
        ->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

```

- In your class:

```php
...
class ArticleController
{
    public function add(Request $request, Response $response, array $args): Response
    {
        ...
        $item = $args['item'] ?? 1;
        $feature = $args['feature'] ?? 'paperbag';
        ...
    }
}
...

```

* * *

Or even you can do one path for each http method and then group the routes like this:

```php
    $app->group('/paper_bags_laminate', function () use ($app) {
        $app->get('', 'ArticleController:get');
        $app->post('', 'ArticleController:add');
    })->addMiddleware($container->get(ValidationMiddleware::class))
      ->addMiddleware($container->get(CSRFMiddleware::class));

    $app->group('/article/{item}/{feature}', function () use ($app) {
        $app->get('', 'ArticleController:get');
        $app->post('', 'ArticleController:add');
    })->addMiddleware($container->get(ValidationMiddleware::class))
      ->addMiddleware($container->get(CSRFMiddleware::class));

```

---

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [May 15, 2020, 8:49am UTC](https://discourse.slimframework.com/t/routing-paths-aliases/4160/7 "2020-05-15T08:49:53Z")

</div>

Thank you so much for your response. However both approaches still require two separate route declarations 🙂 and this is my, maybe not issue but something I wish to avoid. I was hoping there exists a kind of solution (that I am not perhaps aware of) similar to 🙂

```php
    $app->map(['GET', 'POST'], ['/article/{item}/{feature}', '/paper_bags_laminate'], 'ArticleController:add')
        ->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

```

anyway, once again thank you, especially that on the other hand, maybe, once again maybe, you made me convinced for the shorter syntax meaning:

```php
    $app->map(['GET', 'POST'], '/article/{item}/{feature}', 'ArticleController:add')
        ->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

```

instead of:

```php
    $app->map(['GET', 'POST'], '/paper_bags_laminate', function (Request $request, Response $response) {
        return $this->get(ArticleController::class)->add($request, $response, 1, 'paperbag');
    })->setName('article.article.add')
        ->addMiddleware($container->get(ValidationMiddleware::class))
        ->addMiddleware($container->get(CSRFMiddleware::class));

```
