在没有自定义控制器的情况下如何配置一个重定向
有时,一个URL需要跳转到另一个的URL。你可以在控制器中创建一个新的action,这个action的唯一任务就是“重定向”,但使用FrameworkBundle 的 RedirectController
则要简单的多。
你可以重定向到一个指定的路径(如 /about
)或者指定一个路由名称(如 homepage
)。
使用路径重定向 ¶
假如你的程序没有对/
路径指定默认的控制器,你希望把它的请求给重定向到/app
。那么你需要使用urlRedirectAction()
这个action来把它重定向到新的url:
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: annotation
prefix: /app
# redirecting the root
root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /app
permanent: true |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="Http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">
<!-- load some routes - one should ultimately have the path "/app" -->
<import resource="@AppBundle/Controller/"
type="annotation"
prefix="/app"
/>
<!-- redirecting the root -->
<route id="root" path="/">
<default key="_controller">FrameworkBundle:Redirect:urlRedirect</default>
<default key="path">/app</default>
<default key="permanent">true</default>
</route>
</routes> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// app/config/routing.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
// load some routes - one should ultimately have the path "/app"
$appRoutes = $loader->import("@AppBundle/Controller/", "annotation");
$appRoutes->setPrefix('/app');
$collection->addCollection($appRoutes);
// redirecting the root
$collection->add('root', new Route('/', array(
'_controller' => 'FrameworkBundle:Redirect:urlRedirect',
'path' => '/app',
'permanent' => true,
)));
return $collection; |
在这个例子中,你为/
路径配置了路由,并且让RedirectController
把它重定向到/app
路径。这个permanent
开关,告诉action发出301
HTTP状态码来替代默认的302
状态码。
使用路由重定向 ¶
假设你从WordPress网站迁移到Symfony,你想去重定向/wp-admin
到sonata_admin_dashboard
这个路由。你并不知道路径,只知道路由名称。这可以通过redirectAction()
这个action来实现:
1 2 3 4 5 6 7 8 9 10 11 |
# app/config/routing.yml
# ...
# redirecting the admin home
root:
path: /wp-admin
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: sonata_admin_dashboard
permanent: true |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">
<!-- ... -->
<!-- redirecting the admin home -->
<route id="root" path="/wp-admin">
<default key="_controller">FrameworkBundle:Redirect:redirect</default>
<default key="route">sonata_admin_dashboard</default>
<default key="permanent">true</default>
</route>
</routes> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// app/config/routing.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
// ...
// redirecting the root
$collection->add('root', new Route('/wp-admin', array(
'_controller' => 'FrameworkBundle:Redirect:redirect',
'route' => 'sonata_admin_dashboard',
'permanent' => true,
)));
return $collection; |
因为你是重定向路由,而不是路径,redirect
action所需的route
选项被调用,取代了urlRedirect
action所需的path
。