Routing in Symfony2

前端 未结 10 1664
抹茶落季
抹茶落季 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 04:56

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

    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-]+" 
    
    0 讨论(0)
  • 2021-02-05 05:03

    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).

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

    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?

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

    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

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

    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.

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