Symfony language when routing resources - symfony

I'm trying to use a _locale variable in a routing, and if is not set use the default locale. The routing looks like this:
#/app/config/routing.yml
xxx_yyy_zzz:
resource: "#XYZBundle/Resources/config/routing.yml"
prefix: /orientation/{_locale}
defaults: { _locale: %locale% }
What I'm trying to do is to be able to use "domain.com/orientation/WhateverLanguage" or "domain.com/orientation", but it doesn't work.
When I type "domain.com/orientation" I get a "No route found for "GET /orientation".
Am I missing something? Can this be done?
Thanks !

Maybe this cookbook from the Symfony doc will help you.
http://symfony.com/doc/master/cookbook/routing/service_container_parameters.html
Grtz

Related

Add optional _locale to routes

I'd like to prefix all my urls by an optional _locale. BTW, I'm handling fr and en.
The point is, when _locale is given I'll use it thanks to symfony Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest, if not I'll use the locale saved in the database thanks to my own listener (priority: 15) which runs immediately after the previous one.
What I've tried so far is the following:
app/config/routing.yml
entrypoint:
resource: 'locale_prefixed_routing.yml'
prefix: /{_locale}
And locale_prefixed_routing.yml imports bundles routings.
/fr/url/example works but /url/example does not and returns 404 page.
How do I tell symfony to use the locale in the url, if does not exist use the one in the database?
I suppose your routes are defined like this in your locale_prefixed_routing.yml :
my_route:
path: /
defaults:
Actually routes like /url/example without any locale are not defined in your routing because you ask to prefix all the routes loaded in locale_prefixed_routing.yml by the locale. So it is normal to get an error when requesting such a route without locale.
You can try to make another routing file named non_localized_routing.yml containing the same routes except that you will use specific defaults.
Example :
route_example:
path: /url/example
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /{_locale}/url/example
permanent: true
Tell me if it permits you to get rid of your problem, I haven't test it yet.
For me it sounds weird to store the locale in the database when the locale is not provided by the user. You can simply add a default one:
framework:
default_locale: en

Symfony2 & EmberJS: how to make both prod and dev (/app_dev.php/...) urls working?

Say my application currently responds to the /products route just fine. How do I setup Ember.JS so that /app_dev.php/products would be the same route?
If I understand your problem right, you need to generate Symf route via js instead of static "/products" route?
If so you should use FOSJsRoutingBundle https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
Just set option "expose" to your routing as:
app/config/routing.yml
my_products:
pattern: /products
defaults: { _controller: ProductBundle:Products:index }
options:
expose: true
then you can generate the route depends on env from javascript as:
Routing.generate('my_products')

How to set default controller and action in symfony2?

How can I set default controller and action in symfony2. Where to configure this settings.
Which annotation do you use?
My default Route is in the file app/config/routing.yml
_index:
pattern: /
defaults: { _controller: ACMEUserBundle:Default:index }
If you have installed with DemoBundle there is a Default Route in the file src/Acme/DemoBundle/Resources/config/routing.yml.
Look the Link it's show some idea to you
http://symfony.com/doc/current/book/routing.html
cant run the controller with symfony 2

symfony/Routing - import yml resource twice

I'm trying to enable optional locale placeholder for all the routes without duplicating everything. My routes look like this:
site:
prefix: /
resource: "routes-site.yml"
site_i18n:
prefix: /{_locale}
resource: "routes-site.yml"
defaults: {_locale: pl}
requirements:
_locale: 'en'
But I get only site_i18n working. Why I cannot import same resource multiple times?
It's Symfony 2.2
Because the routes have same route name, the later import overrides routes from the first one.
Have a look at: BeSimpleI18nRoutingBundle. It allows you even localize the whole path, but in this case, you will just need localize prefix.

Ordering of routes using annotations

The usual solution when you create routes in symfony and you want to have one route like
/{username}
so that it does not conflict with other routes like /login or /info is just to put that route as your last route in your routing.yml file. Since all the other routes take precedence this conflict is avoided. But how can you do this if you define your routes as annotations in your controllers? Is there any way to specify the ordering of this routes in this case?
In the context of a controller, the order of action methods defines the order of routes. In the context of the whole application, you can import each controller explicitly to control the order, for example:
Home:
resource: "\Vendor\Controller\HomeController"
type: annotation
Security:
resource: "\Vendor\Controller\SecurityController"
type: annotation
security.log_out:
pattern: "/logout"
User:
resource: "\Vendor\Controller\UserController"
type: annotation
I can't comment on the answer, so I will leave how I had to write it in Symfony 2.3 to get it to work:
Home:
resource: "#AcmeBundle/Controller/HomeController.php"
prefix: /home #optional
type: annotation
Notice the change of "\" to "/" and using .php at the end of controller name.
You need to overwrite the rule for that route at the end of your routing.yml. You can do this by using the same name for the route as the one that is automatically created by the annotation. You can find the name with the console command:
php app/console debug:router
So in your routing.yml as the last line you will add:
the_name_of_the_route_as_found_with_appconsole_debug_router:
path:/{username}
defaults: { _controller: YourBundle:YourController:the_action_to_use }
By using the same name it is given by default you will override the original. Don't forget other options that might be important. Like a default value or null for {username} or the method.
You could also remove completely the route from the annotation for the controller. It is not required anymore as it is overwritten anyway :)

Resources