# Redirect to another route

**URL:** <https://discourse.slimframework.com/t/redirect-to-another-route/3582>\
**Category:** Questions\
**Created:** [October 25, 2019, 7:45am UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582 "2019-10-25T07:45:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robo2k](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@robo2k](https://discourse.slimframework.com/u/robo2k)\
**Post date:** [October 25, 2019, 7:45am UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/1 "2019-10-25T07:45:22Z")

</div>

How can i redirect to another route handler inside the closure?

---

<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:** [October 25, 2019, 11:48am UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/2 "2019-10-25T11:48:13Z")

</div>

If there is the `$app` instance available then like this: [http://www.slimframework.com/docs/v4/objects/routing.html#redirect-helper](http://www.slimframework.com/docs/v4/objects/routing.html#redirect-helper)

In all other (default) cases, the $response object should be used:

Within a route closure:

```php
$response = $response->withStatus(302);

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

```

Within a middleware:

```php
$response = $responseFactory->createResponse(302); 

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

```

**Tip 1:** Instead of using a fix number, a constant should be used for the http status codes:

```php
// composer require fig/http-message-util

\Fig\Http\Message\StatusCodeInterface::STATUS_FOUND;

```

**Tip 2** : Instead using a fix url use the **RouteContext** object and named routes for the url:

```php
use Slim\Routing\RouteContext;

// ...

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

return $this->responseFactory->createResponse()
    ->withHeader('Location', $url)
    ->withStatus(302);

```

---

<div class="post-metadata">

**Author:** ![robo2k](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@robo2k](https://discourse.slimframework.com/u/robo2k)\
**Post date:** [October 25, 2019, 12:51pm UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/3 "2019-10-25T12:51:48Z")

</div>

Many thanks for answer!  
So if i use this

> [@odan](#):
>
> $response = $responseFactory-\>createResponse(302); return $response-\>withHeader(‘Location’, ‘my url’);

It will redirect to another handler, insted of returning response to a user?

---

<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:** [October 25, 2019, 1:47pm UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/4 "2019-10-25T13:47:33Z")

</div>

Yes, the response goes back to the user, and the browser redirects to user to the new url resp. another (route) handler.

---

<div class="post-metadata">

**Author:** ![robo2k](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@robo2k](https://discourse.slimframework.com/u/robo2k)\
**Post date:** [October 25, 2019, 2:58pm UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/5 "2019-10-25T14:58:36Z")

</div>

It is not possible to do it server side only?

---

<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:** [October 25, 2019, 8:41pm UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/6 "2019-10-25T20:41:15Z")

</div>

Yes, by using the same action handler for two different urls. But then the client (browser) will not be redirected to the real 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:** [January 8, 2021, 10:32am UTC](https://discourse.slimframework.com/t/redirect-to-another-route/3582/7 "2021-01-08T10:32:50Z")

</div>

**Update:** For redirects you can also use the `$app->redirect(...)` helper method.

Read more: [Redirect helper](https://www.slimframework.com/docs/v4/objects/routing.html#redirect-helper)
