Search code examples
routessymfony1

Symfony routing problem, route being ignored


I have the following in my app/frontend/config/routing.yml:

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

default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/*

sf_guard_register:
  url:   /register
  param: { module: user, action: register }

sf_guard_signin:
  url:   /login
  param: { module: sfGuardAuth, action: signin }

sf_guard_signout:
  url:   /logout
  param: { module: sfGuardAuth, action: signout }

sf_guard_password:
  url:   /request_password
  param: { module: sfGuardAuth, action: password }

When I try the /register route, I get this:

Action "register/index" does not exist.

I should specify, I don't have a "register" module, and my user module is working well if called manually. Plus the other routes are working fine.

Any ideas are welcomed, thank you!


Solution

  • In line with jeremy's answer:

    Because your default_index route is placed above, going to /register causes this route to match. Since it is located before the sf_guard_register route, only the first match "counts".

    Your routing.yml should look like this:

    sf_guard_register:
      url: /register
      param: { module: user, action: register }
    
    sf_guard_signin:
      url: /login
      param: { module: sfGuardAuth, action: signin }
    
    sf_guard_signout:
      url: /logout
      param: { module: sfGuardAuth, action: signout }
    
    sf_guard_password:
      url: /request_password
      param: { module: sfGuardAuth, action: password }
    
    homepage:
      url: /
      param: { module: main, action: index }
    
    default_index:
      url: /:module
      param: { action: index }
    
    default:
      url: /:module/:action/*
    

    Also, there are a number of best-practises that you might want to follow:

    1. Disabling the default routes and relying solely on named routing such as @homepage is faster than having symfony figure out which route to match when using url_for or link_to helpers. Therefore, disable the default routing and create the routes yourself. Then, use the name of the route when generating URLs.
    2. The routes used most often should be placed at the top, for example if you have a register link on all pages, you should place sf_guard_register at the top.

    Just my 5 cents - hope you find it useful ;-)