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 }
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 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
I have my own routing system I would like to send each call to one controller and in the controller I will detect which route/content I will display.
How to write this in the routing.yml so I can send each call to one controller?
Thanks!
I think you could do it with a catch all route like
catch_all:
path: /{catch_all}
defaults:
_controller: Your:Controller:AndAction
requirements:
catch_all: "[\s\S]+"
That should catch all paths and pass them into your controller as the "catch_all" parameter, from there you could do what ever you want with it.
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.
When i call my bundle as a service, everything working fine.
When i give a route to my bundle's controller, __contstruct stops working and the variables coming from config.yml file reasoning this.
These are warnings, but i need to get work to set my variables.
Warning: Missing argument 1 for
ATL15\GoogleAnalyticsBundle\Controller\GoogleAnalyticsController::__construct(),
called in
/var/www/vsy-bio/app/cache/dev/jms_diextra/controller_injectors/ATL15GoogleAnalyticsBundleControllerGoogleAnalyticsController.php
on line 13 and defined in
/var/www/vsy-bio/src/ATL15/GoogleAnalyticsBundle/Controller/GoogleAnalyticsController.php
on line 22
You need to call your controller as a service aswell in your routing like this:
hello:
pattern: /hello
defaults: { _controller: acme.hello.controller:indexAction }
See the documentation chapter How to define Controllers as Services.