Use array from parameters.yml in routing.yml - symfony

I got a locales declaration in my parameters.yml
parameters:
locale: en
locales: [ en, de, fr, it, es, pt, ru, ja, zh ]
and want to reuse the locales param in routing.yml
homepage_locale:
pattern: /{_locale}
defaults: { _controller: SiteBundle:World:index }
requirements: { _locale: %locales% }
But this obviously results in
The container parameter "locales", used in the route configuration value
"%locales%", must be a string or numeric, but it is of type array.
Is there a way to reuse this param or do I really have to write the locales as a string to satisfy this yaml/regex format, like this:
en|de|fr|it|es|pt|ru|ja|zh

In src/AppBundle/DependencyInjection/AppExtension.php
you can add in load function this code
$languages = $container->getParameter('languages');
$container->setParameter('languages_string', implode('|', $languages));
Then you will can use in your annotation
* requirements={"_locale": "%languages_string%"},
It prevent from duplicate parameters in your config file.

What about it:
parameters:
locale: en
locales: en|de|fr|it|es|pt|ru|ja|zh

Maybe you must try
_locale: en|de|fr|it|es|pt|ru|ja|zh
Good look

Related

Optional Prefix part in Route

I'm trying to create a rather complex-route schema and I'm stuck.
Routes that I wan't to match
/books indexAction
/books/show showAction
/books/authorname indexAction
/books/authorname/show showAction
Current Setup
Routing.yml
actions:
resource: routing/actions.yml
prefix: /books/{authorname}
requirements:
authorname: ".*"
defaults:
authorname: ''
routing/actions.yml
books_index:
path: ""
defaults: { _controller:bookController:indexAction }
books_show:
path: "/show"
defaults: { _controller:bookController:showAction }
This Setup currently matches only:
/books/ indexAction
/books/show showAction
/books/authorname indexAction
/books/authorname/show showAction
but not
/books
The reasons why I split those routes into two files is that in reality the actions contain much more routes, also there is other routing-categories then the actions.
I know I could define the /books route manually without a prefix but I want to avoid this as this schema will be used for many different modules.
You need a /book prefix in any case.
One solution is to define separate entries in your routing.yml for that. One with /books prefix and the second with /books/{authorname}.
index:
resource: routing/actions.yml
prefix: /books
show:
resource: routing/actions.yml
prefix: /books/{authorname}
It's not so elegant, but in this case you can get rid of extra requirements section.
Hope this helps!

Define route without ending slash when importing route in Symfony 2.5?

In file: HardCoreMore/HRAPIBundle/Resources/config/routing.yml
I have imported route like this:
hr_api_company:
resource: "#HRAPIBundle/Resources/config/routing/company.yml"
prefix: /company
And in file: HardCoreMore/HRAPIBundle/Resources/config/routing/company.yml
I have defined route for creating company like this:
hardcoremore_hr_api_company_create:
pattern: /
defaults: { _controller: HRAPIBundle:Company:create }
methods: [POST]
Now the route is matched with following url:
POST company/
but it is not matched when I called it with:
POST company
How can I define route without ending slash when importing route and prefixing it?
this is not possible, see : https://github.com/symfony/symfony/issues/4322
I hope this has helped

Route with optional parameter is not working in Symfony 2

I am experiencing some issues with optional parameters in my routes. I think I did it properly according to the documentation but still it is not working.
So I have the following route defined:
test_wizard:
pattern: /test/wizard/{testName}/{step}/
defaults: { _controller: TestBundle:Wizard:wizard, step: 1 }
and would like the route to be able to be called by /test/wizard/someTestName and then fill in the step parameter with the default value of 1 but everytime I call the route just with the test name I get the following instead:
No route found for "GET /test/wizard/someTestName"
When I call the route by /test/wizard/someTestName/1/ itworks just fine. Why is my defined default value for step not working? Any suggestions? Thanks.
It is not possible to make a parameter optional if you have a character after it (/ in your case). You have to define two routes:
test_wizard:
pattern: /test/wizard/{testName}
defaults: { _controller: TestBundle:Wizard:wizard }
test_wizard_optional:
pattern: /test/wizard/{testName}/{step}/
defaults: { _controller: TestBundle:Wizard:wizard, step: 1 }

Symfony2 i18n routing - defining an array of requirements

Is it possible to define an array of locales used in Symfony2 routing rules, so that I won't have to repeat it over and over in every route and alter it for each route separately in case I change the supported languages list?
I know it's very easy to do a quick search & replace in routing.yml, but the issue's scope could be easily expanded so that it provokes a question like this: is it possible to provide this locale array externally (i.e. to have it read from a database)?
For clarification - here is an example of what I have now:
page_show:
pattern: /{_locale}/page/{slug}
defaults: { _controller: myCompanymyBundle:Cms:pageShow }
requirements:
_locale: en|pl
slug: "[^,]+"
news_archive:
pattern: /{_locale}/news/archive
defaults: { _controller: myCompanymyBundle:Cms:newsArchive }
requirements:
_locale: en|pl
And here is an example of what I'd like to have:
page_show:
pattern: /{_locale}/page/{slug}
defaults: { _controller: myCompanymyBundle:Cms:pageShow }
requirements:
_locale: languages
slug: "[^,]+"
news_archive:
pattern: /{_locale}/news/archive
defaults: { _controller: myCompanymyBundle:Cms:newsArchive }
requirements:
_locale: languages
Where languages is an array, be it a YAML array or a PHP array provided externally; it's not really substantial.
This could be very helpful to further ease the configuration of the application I'm writing. I'd like to extract as much configuration as possible into the config.yml file. So that if I decide that, for instance, I want to add a new language quickly and I already support it in the business logic and templating layers, I just have to modify the languages array in config.yml and voila, it's done.
Thats something that bugs me every time. The only thing I could offer is to put your requirement params into app/config/parameters.yml
# src/App/Resources/config/routing.yml
sport:
pattern: /{_locale}/{sport}
defaults: { _controller: App:Vote:vote }
requirements: { _locale: %routing_locales%, sport: %routing_sports% }
# app/config/parameters.yml
parameters:
# routing requirement regex
routing_sports: 'hockey|football' # available sports so far
routing_locales: 'de|fr' # available locales so far

Symfony2 router - how to call annotation route

Simple case. In my all app I'm using Route annotation driver, defined in routing.yml as follows:
MyBundle:
resource: "#MyBundle/Controller/"
type: annotation
prefix: /someprefix
Some action in MyBundle's Ctrl controller looks like:
#Route("/{page}/{status}", name="default_action", defaults={"page" = 1, "status" = "ok"}, requirements={"page" = "\d+"})
public function defaultAction($page, $status) {...}
Now I want to make this action - default action when visiting my web page. I cannot use just #Route("/") because I'm prefixed. So I'm adding to routing.yml:
_welcome:
pattern: /
defaults: { _controller: MyBundle:Ctrl:default }
And there is where problem starts - Symfony2 is calling default controllers action not from annotation but just from action and I get error:
Controller "...:defaultAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
So simply Symfony2 is not obtaining default values from #Route annotation.
Question is: how to call route for _welcome that is aware of #Route?
You've missed defaults in yml settings, should look like this:
_welcome:
pattern: /
defaults: { _controller: MyBundle:Ctrl:default, page: 1, status: ok }

Resources