感谢你来到这里
我真的很激动
盼望,能有你的支持
捐赠可扫描二维码转账支付
支付宝扫一扫付款
微信扫一扫付款
(微信为保护隐私,不显示你的昵称)
有时,一个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
。
本文,包括例程代码在内,采用的是 Creative Commons BY-SA 3.0 创作共用授权。