简体   繁体   English

Symfony2,路由自动重定向并自动生成新路径,这可能吗?

[英]Symfony2, routing autoredirect and also auto generate new path, is it possible?

I successfuly created redirecting in my routing, if page is 1, redirect to main controller without URL parameter page. 我成功地在路由中创建了重定向,如果页面为1,则重定向到没有URL参数页面的主控制器。 (SEO prevention for dublicate content). (防止SEO出现重复内容)。 So I have this route rules: 所以我有这条路线规则:

default_blog:
    path:     /                                               
    defaults: { _controller: AcmeBlogBundle:Default:index, page: 1}

default_blog_page_first:
    path:     /page/1
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: default_blog
        permanent: true

default_blog_page:
    path:     /page/{page}
    defaults: { _controller: AcmeBlogBundle:Default:index}
    requirements:
        page: \d+

It works, in my pagination. 它在我的分页中有效。 If i click on my first page (/page/1) i will be redirected on /, but is it possible that it will already transformed in generating path? 如果单击我的第一页(/ page / 1),我将在/上重定向,但是它可能已经在生成路径中转换了吗? Just, if default_blog_page will have parameter page = 1 it will automatically transformed to default_blog ? 只是,如果default_blog_page参数page = 1 ,它将自动转换为default_blog吗?

So, my pagination will looks like this: 因此,我的分页将如下所示:

URL => Page
-------------------
/ => 1 , /page/2 => 2

In any twig template: 在任何树枝模板中:

path('default_blog_page', {'page': 1})

if detects page = 1, it will automatically changed to: 如果检测到页面= 1,它将自动更改为:

path('default_blog')

Is it possible? 可能吗?

Ok I think I understand what you want to do, and yes, it's possible ! 好的,我想我知道您想要做什么,是的,有可能!

Here is the trick. 这是窍门。 Edit the default_blog_page route by this code : 通过以下代码编辑default_blog_page路由:

default_blog_page:
path:     /{type}/{page}
defaults: { _controller: AcmeBlogBundle:Default:index, type: "page", page: 1}
requirements:
    page: \d+
    type: page

With this route, if you set the page param, it will automatically add the default type value, "page" but if it's the default values, it won't. 使用此路由,如果您设置页面参数,它将自动添加默认类型值“ page”,但如果是默认值,则不会。 Although you can remove your redirection routes because this route handle all cases. 尽管您可以删除重定向路由,因为此路由可以处理所有情况。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM