# Redirecting (short form)

**URL:** https://discourse.slimframework.com/t/redirecting-short-form/3985
**Category:** Questions
**Created:** [March 31, 2020, 10:16am UTC](https://discourse.slimframework.com/t/redirecting-short-form/3985 "2020-03-31T10:16:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)
#### Post date: [March 31, 2020, 10:16am UTC](https://discourse.slimframework.com/t/redirecting-short-form/3985/1 "2020-03-31T10:16:00Z")

</div>

Redirecting changed a lot over versions.

**In Slim v2:**

```
$app->redirect($app->urlFor('foo'));

```

**In Slim v3:**

```
$url = $this->router->pathFor('foo');
return $response->withStatus(302)->withHeader('Location', $url);

```

**In Slim v4:**

Set up the router once with

```
$app->addRoutingMiddleware();

```

Then:

```
$routeParser = \Slim\Routing\RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('foo');
return $response->withStatus(302)->withHeader('Location', $url);

```

**Is there anything shorter for v4 that I’d have missed?**

If, to make things shorter and easier to read, I make **a helper class to shorten the syntax, what is the recommended way to access the router** then? e.g. make `$app` a global available to my Helper class? Pass `$request` to the Helper class?

Any tip on the topic is welcome.  
Thanks.

---

<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: [March 31, 2020, 5:43pm UTC](https://discourse.slimframework.com/t/redirecting-short-form/3985/2 "2020-03-31T17:43:50Z")

</div>

Hi @fabswt Good question.

There is a set of [PSR-7 object decorators](https://github.com/slimphp/Slim-Http) providing useful convenience methods, such as `Response::withRedirect($url, $status)`. But you still need the `RouteParser` to create a URL by a route name. So there is no real “shortcut” at all.

---

<div class="post-metadata">

### Author: ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)
#### Post date: [April 1, 2020, 10:24am UTC](https://discourse.slimframework.com/t/redirecting-short-form/3985/3 "2020-04-01T10:24:02Z")

</div>

Hey Odan,

Thanks. I ended up doing it like this:

```php
    /**
     * Redirect.
     *
     * Inspired by Slim's RouteParserInterface.
     *
     * @param string $destination URL or Route name
     * @param array $data Named argument replacement data
     * @param array $queryParams Optional query string parameters
     *
     * @return string
     *
     * @throws RuntimeException If named route does not exist
     * @throws InvalidArgumentException If required data not provided
     */
    public static function redirect($request, $response, string $destination, array $data = [], array $queryParams = []) {
        if (filter_var($destination, FILTER_VALIDATE_URL)) {
            $url = $destination;
                // WOULDDO(): add support for query parameters, if I need it.
        } else {
            $routeParser = \Slim\Routing\RouteContext::fromRequest($request)->getRouteParser();
            $url = $routeParser->urlFor($destination, $data, $queryParams);
        }
        return $response->withStatus(302)->withHeader('Location', $url);
    }

```

---

<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: [November 6, 2023, 8:06pm UTC](https://discourse.slimframework.com/t/redirecting-short-form/3985/4 "2023-11-06T20:06:40Z")

</div>

A post was split to a new topic: [Inject PSR-7 object decorators to route in Slim 4](https://discourse.slimframework.com/t/inject-psr-7-object-decorators-to-route-in-slim-4/5739)
