How to Configure a Redirect without a custom Controller

Sometimes, a URL needs to redirect to another URL. You can do that by creating
a new controller action whose only task is to redirect, but using the
RedirectController of
the FrameworkBundle is even easier.

You can redirect to a specific path (e.g. /about) or to a specific route
using its name (e.g. homepage).

Assume there is no default controller for the / path of your application
and you want to redirect these requests to /app. You will need to use the
urlRedirectAction()
action to redirect to this new url:

YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# app/config/routing.yml# load some routes - one should ultimately have the path "/app"AppBundle:resource:'@AppBundle/Controller/'type:annotationprefix:/app# redirecting the rootroot:path:/defaults:_controller:FrameworkBundle:Redirect:urlRedirectpath:/apppermanent:true

// app/config/routing.phpuseSymfony\Component\Routing\RouteCollection;useSymfony\Component\Routing\Route;$routes=newRouteCollection();// load some routes - one should ultimately have the path "/app"$appRoutes=$loader->import("@AppBundle/Controller/","annotation");$appRoutes->setPrefix('/app');$routes->addCollection($appRoutes);// redirecting the root$routes->add('root',newRoute('/',array('_controller'=>'FrameworkBundle:Redirect:urlRedirect','path'=>'/app','permanent'=>true,)));return$routes;

In this example, you configured a route for the / path and let the
RedirectController redirect it to /app. The permanent switch
tells the action to issue a 301 HTTP status code instead of the default
302 HTTP status code.

Assume you are migrating your website from WordPress to Symfony, you want to
redirect /wp-admin to the route sonata_admin_dashboard. You don't know
the path, only the route name. This can be achieved using the
redirectAction()
action: