问题
I want to create a route, that matches on an array.
example:
Name_show: pattern: /Name/{names} defaults: { _controller: testBundle:Name:showNames }
where {names}
holds an array of names, like:
array([1] => 'Thomas', [2] => 'Anton', [3] => 'Berta');
How to handle this? Symfony 2.0 responses with
Warning: preg_match() expects parameter 2 to be string, array given
can't find any solution in symfony doc.
回答1:
You can use it as a string and just serialize()
the content before and after.
As far as I know symfony2 doesn't allow arrays in routing, how would the URL Look with an array in? A URL has to be a string, so serializing the array and passing it as a string is probably a better option.
回答2:
I agree with @JamesHalsall about serializing. I would also add that the serialized string should then be urlencoded before being used as a GET parameter.
回答3:
You have to validate the route, so either use some contrived event handling (see the kernel.controller event ) or use a controller that simply redirects to a new URL if the Name is in the array or displays an error page.
Also you could experiment with the ChainRouter that is a replacement for the default Symfony2 router (supports Doctrine-based routing and such).
来源:https://stackoverflow.com/questions/8354634/symfony-2-0-how-to-create-route-with-array-parameters