Prefix Error in Production | Symfony 5 | No route found - symfony

I got the following strange behaviour in production. the twig command {{ path('recherche') }} thows a critical error log but does not stop the rendering process:
Unable to generate a URL for the named route "recherche"
It seems that it comes from the locale. 2 Prefixes are defined in annotations.yml :
resource: ../../src/Controller/
type: annotation
prefix:
html: ''
amp: '/amp'
when I add the _locale as param {_locale: 'html' }, there's no error
Any help would be great.

Probably you are misusing the routing rules.
Prefixes (and paths) are single-valued parameters.
Multiple values are accepted only on in case of Localized Routes (that's why does not throw any error when passing a _locale parameter).
The only thing you can do without extending the builtin router is to specify two different route prefixes like:
web:
resource: ../../src/Controller/
type: annotation
amp:
resource: ../../src/Controller/
type: annotation
name_prefix: 'amp_'
prefix: '/amp'
And then call a custom twig function that evaluates the context (amp or not) and calls the router with the correct target route name (with or without the 'amp_' prefix)

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

FOSRestBundle doesn't load routes

I'm trying to make a REST API using FOSRestBundle and I'm getting some errors due to routing. This is how I test the routes. With this code in routing.yml I get this error:
1st test:
routing.yml
device_id_api:
type: rest
path: /device/{imei}/getid
resource: "Device\DeviceBundle\Controller\DeviceRestController"
Error
FileLoaderLoadException: Cannot import resource
"/var/www/html/src/Device/DeviceBundle/Resources/config/routing.yml"
from "/var/www/html/app/config/routing.yml". (The routing
file
"/var/www/html/src/Device/DeviceBundle/Resources/config/routing.yml"
must not specify both the "resource" key and the "path" key for
"device_id_api". Choose between an import and a route definition.)
2nd test:
`routing.yml
device_id_api:
type: rest
prefix: /device/{imei}/getid
resource: "TaxiBooking\Device\DeviceBundle\Controller\DeviceRestController"
Error
No route found for "GET /device/2147483647/getid"
What is wrong on that routes? I clear the cache several times and the error remains. Any help?
The prefix /device/{imei}/getid is exactly that, a prefix.
The actions in your controller like (for, example) getUsersAction will then be added to this prefix to create paths like /device/{imei}/getid/users [GET].
It's all explained in the docs minus your usage of the prefix, but that just means that it added to the start of the path auto-generated by the RoutingLoader.

Symfony 2 multiple bundles annotation type routing

I have a Symfony 2.3.1 application with two bundles. Each bundle contains Resources/config/routing.yml configuration file:
mobile:
resource: "#MyMobileBundle/Controller"
type: annotation
and
admin:
resource: "#MyAdminBundle/Controller"
type: annotation
This is app/config/routing.yml:
_mobile:
resource: "#MyMobileBundle/Resources/config/routing.yml"
prefix: /mobile
_admin:
resource: "#MyAdminBundle/Resources/config/routing.yml"
prefix: /admin
And app/config/routing_dev.yml contains:
_main:
resource: routing.yml
The problem is that each time only /admin/... or /mobile/... paths are available. If only one routing resource included in app/config/routing.yml everything works fine. Has anybody had such problem? Is it correct to set prefixes for different bundles this way?
The command php app/console router:debug is the best way to debug routes in Symfony2.
According to the details you provided everything seems to be correct and you are saying that removing one of the route prefix "fixes" your issue.
Visualizing your routes in an array
_mobile: # defines the prefix /mobile
mobile: # key that defines how you include your controller's route
main: /mobile/main # "main" is the route name which is duplicated below
_admin: # defines the prefix /admin
admin: # key that defines how you include your controller's route
main: /admin/main # this route override the original "main" route
In Symfony2 a route isn't defined by the addition of the prefix name and the route name but solely by the route name. If you have two routes named main then Symfony2 will only have reference of one.
In the case above, only /admin/main will be accessible because it overrode /mobile/main.
In short, you can't have two routes with the same route name.
So the best solution to fix the example above is by prefixing the route name with a key (much like namespacing):
_mobile:
mobile:
mobile_main: /mobile/main
_admin:
admin:
admin_main: /admin/main
Now you have two routes named admin_main and mobile_main which don't overlap each other.

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 :)

Routing : Import a routing.yml resource with a prefix and requirements not work

I try to load a localized_routes.yml inside the main routing.yml file with :
localized_routes:
resource: "routing_localized.yml"
prefix: /{_locale}
requirements: { _locale : fr|es|it|de }
But it doesnt work at all, the requirements are not in the generated URL checks and so I can use any locale I want .... I see in the Symfony source that only the prefix are taken, not requirements or defaults values, is it an issue ?

Resources