I need to translate my application. I set the prefix up for all my routes.
controllers:
resource: ../../src/Controller/
type: annotation
prefix: /{_locale}
It works but I would like to set a default locale. For example, some routes are set to be called by ajax request. It has to work for all locales. For example /foo/data is one of my route and is defined by /{_locale}/foo/data. I would like to force all routes to have a default locale to force the locale if it's not provided.
Is there a solution for my issue or should I redefine the urls of my ajax requests manually.
Try this :
prefix: /{_locale, locale: default}
Where default is your default country
Related
I am working with Symfony 6. The configuration of a route allows to add extra parameters which are passed to the controller:
index:
path: /somepage
controller: App\Controller\PageController::index
defaults:
title: 'Hello world!'
Is it possible to use a service as parameter? Something like:
defaults:
foo: '%some.container.parameter%' << Works, use container parameter?
logger: '#logger' << FAILS, use services as parameter?
This example does not work. While container parameters are correctly resolved, the services parameter is interpreted as string #logger instead of being resolved to the logger service.
Of course one could inject the complete service container into the PageController and use $this->container->get($logger); to get the services by its id. However, I found several sources, that injecting the services container is a bad idea which should be avoided.
So, is it possible to use a services as routing parameter?
Background
I would like to use a service parameter to make the controller more flexible when being used at different places.
For example the RedirectController can be used to create simple redirects without having to create a different controller for each redirected route.
Assume that the redirection of some routes is critical for some reason and needs more verbose logging and for example an alert notification via email.
Instead of creating different controllers and injecting different loggers, one could solve this by using different loggers as parameter:
legacy_one:
path: /somepage
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController
defaults:
logger: '#defaultLogger'
legacy_two:
path: /otherpage
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController
defaults:
logger: '#alertLogger'
I have to work on a Symfony project that I never met before. There is a route that I'd like to figure out what script generates the result. Normally int the routing.yml the the _controller in the defaults tells this, but this route does not have any controller associated with:
vendorname_admin_generate_external_site_admin:
path: /admin/
host: "{current}.%vendorname_academic_base_host%"
defaults: {current: %vendorname_academic_base_subdomain%}
The two parameters are:
vendorname_academic_base_host: vendordomain.com
vendorname_academic_base_subdomain: developmentserver-vpn
Could you help me to understand what does this route definition do?
Because of the fact it is defining a specific host (ie, a domain name) as part of the route, makes me think that it's just an alias to an external website, that isn't actually in the same codebase. -- Also the fact its called 'external_site_admin'.
I've just added a similar route with a specific host, mine is still in the same codebase, but under a specific, shorter URL, but I defined my route as a template that I can use to setup as an alias.
I use symphony ยง and I manage my routes in routing.yml function,
How can I use mutliples routes for same controller function. I know I can do it with annotation but I don't manage my routes in annotation.
You can have multiple routes for the same controller in routing.yml as in annotations.
If there are multiples routes matching the url, the firt one win, as explained in the doc.
The problem
When we define in routing.yml:
my_controllers:
resource: "#MyBundle/Controller/"
type: annotation
prefix: /
and:
my_api:
resource: 'MyBundle\Controller\ApiController'
type: rest
prefix: /api
name_prefix: my_api_
options:
expose: true
We get duplicated routes in NelmioApiDoc:
The question
How we should do this so routes from ApiController won't be duplicated?
exclude ApiController from "wildcard" resource import?
import all other controllers individually?
other solution?
You get duplicated entries in Nelmio ApiDoc because there are actually duplicated routes in Symfony. Execute this command to see every route in Symfony, including those of your API (that will appear both with the /api prefix and without it):
php app/console router:debug
As Yann suggests, I think the best approach is to create a separate directory for your API Controllers (something like #MyBundle/ControllerRest/) and place them there. Then, don't forget to update the resource route under my_api section in your routing.yml and the namespaces in your API Controllers.
This way you get rid of the duplicated routes and thus ApiDoc will only show one route per call, instead of two.
I actually use automatic route generation for my api Rest, using the FOSRESTBundle, also I use NelmioApiDocBundle to generate the api doc.
To generate routes for the api this I have in my routing.yml
users:
type: rest
resource: Project\RESTBundle\Controller\UsersController
But for some actions I want to set my custom routing... If I try to add another route rule for an action it simply ignore it and generate the automatic route.
You have to declare the route with the same name right after the configuration you set. For example, the following works.
users:
type: rest
resource: Project\RESTBundle\Controller\UsersController
get_users:
pattern: /api/users/customUri.{_format}
defaults: { _controller: ProjectRESTBundle:Users:indexAction, _format: json }