Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

php - How to create the route in symfony 2 which maps to external URL?

Referring to this,

http://symfony.com/doc/current/book/routing.html

we can map url pattern to controller and action

app/config/routing.yml

blog_show:
    path:      /blog/{slug}
    defaults:  { _controller: AcmeBlogBundle:Blog:show }

I want to map the path to external url.

app/config/routing.yml

blog_show:
    path:      /blog/{slug}
    defaults:  "www.example.com/blog"

The requirement is, my current website is in kohana, I am porting it gradually to symfony 2. For my symfony2 app kohana URL are like external urls, I want to configure these urls in routing and use them in standard way,

e.g. in Twig,

<a href="{{ path('blog_show'}}">
  Read this blog post.
</a>

So later on when I port my pages to Symfony, I will have to change only routing file so that I could use same blog_show key to refer to url and I wont' have to change all the files where I have used urls.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this by using one of the Symfony framework controllers although I'm not sure how this would work with parameters:

blog_show:
    path: /blog/{slug}
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: "http://example.com/blog"
        permanent: true

Note that path: /blog/{slug} grabs the slug directly, but path: "http://example.com/blog/{slug}" doesn't work.

Source: http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...