Match an exact path with Symfony 2 not just the prefix

1.3k Views Asked by At

I have the following routes

hekdb_login_show:
    path: /Login/showForm
    defaults: { _controller: HEKdbBundle:Login:showForm }

hekdb:
  path: /
  defaults:
    _controller: FrameworkBundle:Redirect:redirect
    route: hekdb_login_show
    permanent: true

(Actually there are more, but these two are sufficient.) The last route is supposed to redirect everyone that comes to the website with nothing more than "/" after the hostname is redirected to the login form.

The problem is that the path "/" in the last routing rule matches every path, because every path starts with a "/". The result is an infinite redirection loop.

I read that path is interpreted as a regular expression, so I tried the pattern "^/$". But this resulted into a PHP error. Then I tried escaping the special regexp symbols "^" and "$", i.e. "\^/\$". But this did not work either.

1

There are 1 best solutions below

1
On

Include this route last to pick up everything that has fallen through any existing routes. I think that is what you actually want.

zayso_core_unknown:
    pattern:  /{url}
    defaults: { _controller: ZaysoAreaBundle:Public:index, url: "unknown" }
    requirements:
       url: ".+"

Don't remember where I got it from but it does the job.

And you are mistaken about / matching everything. I often have this as my very first route:

zayso_core_index:
    pattern:  /
    defaults: { _controller: ZaysoAreaBundle:Public:index }

It works as expected.

You might want to mess a bit with app/console router:match to see what happens.