Routing in Symfony2

2020-05-25 07:36发布

How to setup default routing in Symfony2?

In Symfony1 it looked something like this:

homepage:
  url:   /
  param: { module: default, action: index }

default_symfony:
  url:   /symfony/:action/...
  param: { module: default }

default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/...

10条回答
对你真心纯属浪费
2楼-- · 2020-05-25 07:52

Create a default route is not a good way of programming. Why? Because for this reason was implemented Exception. Symfony2 is built just to do right things in the right way.

If you want to redirect all "not found" routes you should use exception, like NotFound404 or something similar. You can even customise this page at your own.

One route is for one purpose. Always. Other think is bad.

查看更多
对你真心纯属浪费
3楼-- · 2020-05-25 07:52

You could create your own bundle that handled all requests and used URL parameters to construct a string to pass to the controller's forward method. But that's pretty crappy, I'd go with well defined routes, it keeps your URLs cleaner, and decouples the URL and controller names. If you rename a bundle or something, do you then have to refactor your URLs?

查看更多
何必那么认真
4楼-- · 2020-05-25 08:00

I don't think it's possible with the standard routing component. Take a look to this bundle, it might help : https://github.com/hidenorigoto/DefaultRouteBundle

查看更多
淡お忘
5楼-- · 2020-05-25 08:01

// Symfony2 PR10

in routing.yml:

default:
    pattern:  /{_controller}

It enables you to use this kind of urls: http://localhost/MySuperBundle:MyController:myview

查看更多
登录 后发表回答