# Redirect with Slim 4

**URL:** <https://discourse.slimframework.com/t/redirect-with-slim-4/4689>\
**Category:** Questions\
**Created:** [March 9, 2021, 12:20am UTC](https://discourse.slimframework.com/t/redirect-with-slim-4/4689 "2021-03-09T00:20:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![igor](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/igor/32/771_2.png) [@igor](https://discourse.slimframework.com/u/igor)\
**Post date:** [March 9, 2021, 12:20am UTC](https://discourse.slimframework.com/t/redirect-with-slim-4/4689/1 "2021-03-09T00:20:37Z")

</div>

How can I achieve the same result in Slim 4? This was my slim 3 code. Is there such thing as $response-\>withRedirect in slim 4? I couldn’t find anything in documentation.  
`return $response->withRedirect($this->router->pathFor('MyPage'));`

---

<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:** [March 9, 2021, 12:17pm UTC](https://discourse.slimframework.com/t/redirect-with-slim-4/4689/2 "2021-03-09T12:17:47Z")

</div>

[https://www.slimframework.com/docs/v4/objects/response.html#returning-a-redirect](https://www.slimframework.com/docs/v4/objects/response.html#returning-a-redirect)

```php
return $response
  ->withHeader('Location', 'https://www.example.com')
  ->withStatus(302);

```

---

<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 9, 2021, 3:10pm UTC](https://discourse.slimframework.com/t/redirect-with-slim-4/4689/3 "2021-03-09T15:10:55Z")

</div>

To create a redirect by a given route name:

```php
use Slim\Routing\RouteContext;

$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('MyPage');

return $response
  ->withHeader('Location', $url)
  ->withStatus(302);

```

- [Upgrade Guide - Slim Framework](https://www.slimframework.com/docs/v4/start/upgrade.html#new-dispatcher--routing-results)
- [Retrieving Current Route - Slim Framework](https://www.slimframework.com/docs/v4/cookbook/retrieving-current-route.html)
- [Request - Slim Framework](https://www.slimframework.com/docs/v4/objects/request.html#route-object)
- [Responder](https://github.com/odan/slim4-skeleton/blob/master/src/Responder/Responder.php#L100) example
