Routing in Symfony2
Solution 1
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: ".+"
Solution 2
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
Solution 3
// Symfony2 PR10
in routing.yml:
default:
pattern: /{_controller}
It enables you to use this kind of urls: http://localhost/MySuperBundle:MyController:myview
Solution 4
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).
Solution 5
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-]+"
Related videos on Youtube
umpirsky
Updated on July 09, 2022Comments
-
umpirsky almost 2 years
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/...
-
umpirsky about 13 yearsAnother way is to do it with annotations bundles.symfony-reloaded.org/frameworkextrabundle/… Still no default route.
-
Denis Gorbachev about 13 yearsNo, it's possible. See the answer below.
-
Eno over 12 yearsI think what's odd, is that if you generate your bundle using the console tools, it creates a default controller with routing defined via annotations. But the official book makes no mention of routing via annotations - there's no mention in the book. If this is the default setup, I would think it should be pointed out and documentation (or links to it) provided.
-
Peter O. about 12 yearsBASTA''s comment: "@Marc Morera Merino: consider this use case - a blog with default view, single post view, guestbook, guestbook entry form etc. For the default view, I don't want the URL to be of the form
http://example.com/blog/slug
, but simplyhttp://example.com/slug
. This will be the default view of the site, and certainly nothing that can be conceived as 'exception'". -
domguinard almost 11 yearsWorked, cheers but just to enhance: "Default" is the classname of your controller (first letter capitalized without the "Controller" part).
-
JamesHalsall over 10 yearsAlso, it is completely reasonable to want to have your JS handle the routing and dynamically load the application view, in this case all routes would serve a base layout and your JS router would determine the URL and render the view. This calls for a catch all route
-
Luke over 10 yearsThere is a problem with the code presented in this answer, it needs a route name. Currently
pattern:
is assumed to be the route name andpattern:
,defaults:
andrequirements:
need to indented at the same level. -
h00ligan over 9 yearsMarc, I guess you haven't heard about SEO, changing routes is sometimes necessary when restructuring a site and a catch-all may be needed to be able to give a correct redirect (301 Status)
-
Longsight almost 8 yearsThanks, that was probably a rush copy/paste job from my routes file!