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:45

If you want to create a "catch all", your best bet would be to hook on the KernelEvents::EXCEPTION event. This event gets triggered whenever an Exception falls through to the HttpKernel, this includes the NotFoundHttpException thrown when the router cannot resolve a route to a Controller.

The effect would be similar to Symfony's stylized 404 page that gets rendered when you send the request through app_dev.php. Instead of returning a 404, you perform whatever logic you're looking to.

查看更多
一纸荒年 Trace。
3楼-- · 2020-05-25 07:46

It depends... Some of mine look like this:

api_email:
resource: "@MApiBundle/Resources/config/routing_email.yml"
prefix: /

and some look like

api_images:
path:     /images/{listingId}/{width}/{fileName}
defaults: { _controller: ApiBundle:Image:view, listingId: null, width: null, fileName: null }
methods:  [GET]
requirements:
    fileName: .+
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-05-25 07:48

Here is an example: http://docs.symfony-reloaded.org/master/quick_tour/the_big_picture.html#routing

A route definition has only one mandatory parameter pattern and three optionals parameters defaults, requirements and options.

Here's a route from my own project:

video:
    pattern:  /watch/{id}/{slug}
    defaults: { _controller: SiteBundle:Video:watch }
    requirements: { id: "\d+", slug: "[\w-]+" 
查看更多
▲ chillily
5楼-- · 2020-05-25 07:50

Symfony2 standard routing component does not support it, but this bundle fills the gap Symfony1 left:

https://github.com/LeaseWeb/LswDefaultRoutingBundle

It does what you expect. You can default route a bundle using this syntax:

FosUserBundle:
  resource: "@FosUserBundle"
  prefix:   /
  type:     default

It scans your bundle and automatically adds routes to your router table that you can debug by executing:

app/console router:debug

Example of automatically added default routes:

[router] Current routes
Name                          Method Pattern
fos_user.user.login_check     ANY    /user/login_check.{_format}
fos_user.user.logout          ANY    /user/logout.{_format}
fos_user.user.login           ANY    /user/login.{_format}
...

You see that it also supports the automatic "format" selection by using a file extension (html, json or xml).

查看更多
Emotional °昔
6楼-- · 2020-05-25 07:51

Alternatively, you can use @Route annotation directly in a controller file. see https://github.com/sensio/SensioFrameworkExtraBundle/blob/master/Resources/doc/annotations/routing.rst

As for the default routes, I think Symfony2 encourages explicit route mapping.

查看更多
Fickle 薄情
7楼-- · 2020-05-25 07:52

I was looking through the cookbook for an answer to this, and think I've found it here. By default, all route parameters have a hidden requirement that they match any character except the / character ([^/]+), but this behaviour can be overridden with the requirements keyword, by forcing it to match any character.

The following should create a default route that catches all others - and as such, should come last in your routing config, as any following routes will never match. To ensure it matches "/" as well, a default value for the url parameter is included.

default_route:
    pattern: /{url}
    defaults: { _controller: AcmeBundle:Default:index, url: "index" }
    requirements:
        url: ".+"
查看更多
登录 后发表回答