Routing in Symfony2

前端 未结 10 1680
抹茶落季
抹茶落季 2021-02-05 04:30

How to setup default routing in Symfony2?

In Symfony1 it looked something like this:

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

de         


        
相关标签:
10条回答
  • 2021-02-05 05:07

    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.

    0 讨论(0)
  • 2021-02-05 05:14

    // Symfony2 PR10

    in routing.yml:

    default:
        pattern:  /{_controller}
    

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

    0 讨论(0)
  • 2021-02-05 05:15

    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: .+
    
    0 讨论(0)
  • 2021-02-05 05:16

    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.

    0 讨论(0)
提交回复
热议问题