How Symfony 2 manages route conflicts with the same name

Asked

Viewed 70 times

1

I am using Annotations in the project.

In the archive routing.yml I have the following configuration:

acme_store:
    resource: "@AcmeStoreBundle/Controller/"
    type:     annotation
    prefix:   /acme

teste_blog:
    resource: "@TesteBlogBundle/Controller/"
    type:     annotation
    prefix:   /

Notice that in the Bundle AcmeStore I’m using the prefix acme.

In the controlling class DefaultController.php Bundle AcmeStore, I have the following route configured:

/**
 * @Route("/", name="index")
 */
public function indexAction()
{
    return $this->render('AcmeStoreBundle:Default:index.html.twig');
}

In the controlling class DefaultController.php Bundle TesteBlog, I have the following route configured:

/**
 * @Route("/", name="index")
 */
public function indexAction()
{
    return $this->render('TesteBlogBundle:Default:index.html.twig');
}

Note that the property name is equal in the 2 controllers.

On console, when executing command php app/console debug:router is returned the following result:

------- ------- ------- ----- -----
Name    Method  Scheme  Host  Path                               
------- ------- ------- ----- -----
index   ANY     ANY     ANY   /    

There shouldn’t be one more line?

------- ------- ------- ----- -----
Name    Method  Scheme  Host  Path                               
------- ------- ------- ----- -----
index   ANY     ANY     ANY   / 
index   ANY     ANY     ANY   /acme

If even using a prefix we can not use the same name for different routes, what are the best practices when we choose a name for the route?

I ask this because it is possible to install third party Bundles and this can generate conflicts.

2 answers

1

When I name the routes for my application, I usually use the following methodology:

{bundle}_{controller}_{action}

However, you have the option to leave the name empty. This way, the framework determines the name of the route automatically. Then just see the names of the routes in the command app/console router:debug (in the case of Symfony <= 2.8) or bin/console debug:router (in the case of Symfony >= 3.0).

  • Hello Rodrigo, thank you! However I need to give a "name" because I am using Annotations. But using the Bundle name as you posted, already solves problems.

  • So, but like I said, you can let the framework generate the names and then see the names in the above command to use them in the application. :)

  • Ah ok, got it.

1


You should not have the same name to indicate different routes!(just as there is no same Cpf to identify different people) I say different because if they were equal it would not make sense to have two, there is something different between them.

Regarding the good practices you can follow the example of fosUserBundle that uses the name of Bundle always in your routes. EX: fos_user_change_password.

In your case it would be teste_index and acme_index.

How to Match a Route Based on the Host

Browser other questions tagged

You are not signed in. Login or sign up in order to post.