src/App/Routing/RouteRedirectionManager.php
public function addRedirection($oldPath, $newPath)
    {
        if (!is_string($oldPath) && !($oldPath instanceof UriInterface)) {
            throw new InvalidArgumentException(
                'The deprecated path must be a string; received %s',
                (is_object($oldPath) ? get_class($oldPath) : gettype($oldPath))
            );
        }

        $newPath = $this->parseRoute($newPath);
        $this->paths[$oldPath] = $newPath;

        $this->app()->map(
            $newPath['methods'],
            $oldPath,
            function (
                RequestInterface $request,
                ResponseInterface $response,
                array $args = []
            ) use (
                $newPath
            ) {
                if ($newPath['route'] === 404) {
                    return $response->withStatus(404);
                }

                $locale = isset($args['lang']) ? $args['lang'] : null;

                if ($newPath['data_key'] === null) {
                    if (is_array($newPath['route'])) {
                        $route = $this->translator()->translate($newPath['route'], [], null, $locale);
                        if ($route !== null) {
                            $newPath['route'] = $route;
                        } else {
                            $newPath['route'] = reset($newPath['route']);
                        }
                    }

                    return $response->withRedirect($newPath['route'], $newPath['status']);
                }

                if (!isset($newPath['ident'])) {
                    return $response->withStatus(404);
                }

                if (!empty($newPath[$newPath['data_key']])) {
                    $args = array_merge($newPath[$newPath['data_key']], $args);
                }

                $router = $this->get('router');
                if (isset($newPath[$newPath['data_key']]['route_endpoint'])) {
                    $endpoint = $this->translator()->translate(
                        $newPath[$newPath['data_key']]['route_endpoint'],
                        [],
                        null,
                        $locale
                    );

                    $args['route_endpoint'] = $endpoint;
                }

                $uri = $router->pathFor($newPath['ident'], $args);

                return $response->withRedirect($uri, $newPath['status']);
            }
        );
    }
public function addRedirection($oldPath, $newPath)
    {
        if (!is_string($oldPath) && !($oldPath instanceof UriInterface)) {
            throw new InvalidArgumentException(
                'The deprecated path must be a string; received %s',
                (is_object($oldPath) ? get_class($oldPath) : gettype($oldPath))
            );
        }

        $newPath = $this->parseRoute($newPath);
        $this->paths[$oldPath] = $newPath;

        $this->app()->map(
            $newPath['methods'],
            $oldPath,
            function (
                RequestInterface $request,
                ResponseInterface $response,
                array $args = []
            ) use (
                $newPath
            ) {
                if ($newPath['route'] === 404) {
                    return $response->withStatus(404);
                }

                $locale = isset($args['lang']) ? $args['lang'] : null;

                if ($newPath['data_key'] === null) {
                    if (is_array($newPath['route'])) {
                        $route = $this->translator()->translate($newPath['route'], [], null, $locale);
                        if ($route !== null) {
                            $newPath['route'] = $route;
                        } else {
                            $newPath['route'] = reset($newPath['route']);
                        }
                    }

                    return $response->withRedirect($newPath['route'], $newPath['status']);
                }

                if (!isset($newPath['ident'])) {
                    return $response->withStatus(404);
                }

                if (!empty($newPath[$newPath['data_key']])) {
                    $args = array_merge($newPath[$newPath['data_key']], $args);
                }

                $router = $this->get('router');
                if (isset($newPath[$newPath['data_key']]['route_endpoint'])) {
                    $endpoint = $this->translator()->translate(
                        $newPath[$newPath['data_key']]['route_endpoint'],
                        [],
                        null,
                        $locale
                    );

                    $args['route_endpoint'] = $endpoint;
                }

                $uri = $router->pathFor($newPath['ident'], $args);

                return $response->withRedirect($uri, $newPath['status']);
            }
        );
    }
public function addRedirection ( $ oldPath , $ newPath ) { if ( ! is_string ( $ oldPath ) && ! ( $ oldPath instanceof UriInterface ) ) { throw new InvalidArgumentException ( 'The deprecated path must be a string; received %s' , ( is_object ( $ oldPath ) ? get_class ( $ oldPath ) : gettype ( $ oldPath ) ) ) ; } $ newPath = $ this -> parseRoute ( $ newPath ) ; $ this -> paths [ $ oldPath ] = $ newPath ; $ this -> app ( ) -> map ( $ newPath [ 'methods' ] , $ oldPath , function ( RequestInterface $ request , ResponseInterface $ response , array $ args = [ ] ) use ( $ newPath ) { if ( $ newPath [ 'route' ] === 404 ) { return $ response -> withStatus ( 404 ) ; } $ locale = isset ( $ args [ 'lang' ] ) ? $ args [ 'lang' ] : null ; if ( $ newPath [ 'data_key' ] === null ) { if ( is_array ( $ newPath [ 'route' ] ) ) { $ route = $ this -> translator ( ) -> translate ( $ newPath [ 'route' ] , [ ] , null , $ locale ) ; if ( $ route !== null ) { $ newPath [ 'route' ] = $ route ; } else { $ newPath [ 'route' ] = reset ( $ newPath [ 'route' ] ) ; } } return $ response -> withRedirect ( $ newPath [ 'route' ] , $ newPath [ 'status' ] ) ; } if ( ! isset ( $ newPath [ 'ident' ] ) ) { return $ response -> withStatus ( 404 ) ; } if ( ! empty ( $ newPath [ $ newPath [ 'data_key' ] ] ) ) { $ args = array_merge ( $ newPath [ $ newPath [ 'data_key' ] ] , $ args ) ; } $ router = $ this -> get ( 'router' ) ; if ( isset ( $ newPath [ $ newPath [ 'data_key' ] ] [ 'route_endpoint' ] ) ) { $ endpoint = $ this -> translator ( ) -> translate ( $ newPath [ $ newPath [ 'data_key' ] ] [ 'route_endpoint' ] , [ ] , null , $ locale ) ; $ args [ 'route_endpoint' ] = $ endpoint ; } $ uri = $ router -> pathFor ( $ newPath [ 'ident' ] , $ args ) ; return $ response -> withRedirect ( $ uri , $ newPath [ 'status' ] ) ; } ) ; }
Add a redirection.

Accepted formats for $paths:

**Format #1 — Basic Usage**

The "new-path" can be a URI or a named {@see \Slim\Route}.

```
[ 'old-path' => 'new-path' ]
```

**Format #2 — Redirect to front page**

```
[
'old-path' => []
]
```

**Format #2 — Custom HTTP status code**

```
[
'old-path' => [
'route'  => 'new-path',
'status' => 301
]
]
```

**Format #3 — Verbose redirection**

The target URI or route can be multilingual.

```
[
'old-path' => [
'route' => [
'en' => '/fr/new-path'
'es' => '/es/nueva-ruta'
'fr' => '/fr/nouveau-chemin'
],
'route_type' => 'templates',
'status'     => 301
]
]
```

@todo   Add support for explicit {@see \Slim\Route}.
@param  string $oldPath The path to watch for and redirect to $newPath.
@param  mixed  $newPath The destination for $oldPath.
@throws InvalidArgumentException If the path is not a string.
@return void
Add a redirection .