概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:40:25 | 157 次阅读 | 评论: 0 | 来源: 网络整理

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).

Redirecting Using a Path

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 urlRedirect() action to redirect to this new url:

  • YAML
    # 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
    
  • XML
    <!-- 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>
    
  • PHP
    // app/config/routing.php
    use SymfonyComponentRoutingRouteCollection;
    use SymfonyComponentRoutingRoute;
    
    $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;
    

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.

Redirecting Using a Route

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 redirect() action:

  • YAML
    # app/config/routing.yml
    
    # ...
    
    # redirecting the admin home
    root:
        path: /wp-admin
        defaults:
            _controller: FrameworkBundle:Redirect:redirect
            route: sonata_admin_dashboard
            permanent: true
    
  • XML
    <!-- 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>
    
  • PHP
    // app/config/routing.php
    use SymfonyComponentRoutingRouteCollection;
    use SymfonyComponentRoutingRoute;
    
    $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;
    

警告

Because you are redirecting to a route instead of a path, the required option is called route in the redirect action, instead of path in the urlRedirect action.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务