Question

So I got this in my routing.yml:

requirements:
    var1: \d+
    var2: \d+

Both are checked on their own and valid. I need to check the combination of the 2, since the combination is not always valid.

For this case I need to check the relation between 2 objects in the database, the first should be the parent of the second. I can do this in the controller, but I don't really like that implementation. Also, I need this same check for more than 1 route.

How would I add another requirement that checks the combination? Can I define a method in the controller class that would be called?

Or would the best solution be something like:

public function indexAction($var1, $var2)
{
    $result = $this->checkRelation($var1, $var2);
    if ($result) {
        // return errorpage
        return $result;
    }

    // ... 
}
Was it helpful?

Solution 2

The final solution I went with was the following:

  • add a method checkRelation that requires all parameters
  • run a query inside that method to check if everything is ok.
  • return false when there is a problem, return true when values are ok. (alternatively you can return an object or something)
  • in the action I check if the value is false, if so I return a generic "not found" page for the specific controller.

In all this is very similar to what I posted in my initial question.

When using the same checkRelation in multiple controllers it might be a good idea to move it (partially) to a repository-class or something similar to prevent duplication of code/logic.

OTHER TIPS

So as I understand your question, you want the following:

/parent/child/        --> returns 200
/not_parent/not_child --> returns 404

The Symfony2 Routing component doesn't do this natively, but you could extend it.

http://symfony.com/doc/master/cmf/cookbook/using-a-custom-route-repository.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top