Request Format inside an URL not working on Symfony 5 - symfony

I got an issue after upgrading from Symfony 4 to Symfony 5. When I'm trying to call a route without the request format I got an error that no route was found.
Here my Code from the routing.yml
reportbook:
path: /report.{_format}/{user_id}
methods: [GET]
requirements:
_format: html|json|pdf
defaults:
_controller: Controller\ReportController::showAction
_format: html
user_id: ~
These are the urls I tried to open:
example.com/report/12345 --- not working error: route not found
example.com/report.html/12345 --- works
example.com/report.json/12345 --- works
example.com/report.pdf/12345 --- works
Now I want, like specified inside the routing.yml, that the default format is html.
Befor the Upgrade to Symfony 5 everything worked.

Related

Symfony 5.1 route issue

I installed symfony 5.1.3 today and found the route configuration is something broken. I'm not sure if that is intended. But at least it's different from symfony 4.4. I have the follow setting in 4.4 and it works fine.
homepage:
path:
en-us: /
others: /{_locale}/
controller: App\Controller\IndexController::homepage
requirements:
_locale: en-us|zh-cn|zh-hk
The above settings allow me to access domain.com and other mydomain.com/en-us, mydomain.com/zh-cn and mydomain.com/zh-hk successfully.
But now in symfony 5.1, the above setting does not work now. I believe the _locale with special meaning to the system maybe the root cause. Can anyone give me a help?
I think you are using Localized Routes feature incorrectly.
Keys in path are locales. In your case, you have 2 locales: en-us and others. Try opening mydomain.com/others and you will see that it's working.
I suggest splitting this config into 2 routes:
homepage:
path: /
controller: App\Controller\IndexController::homepage
homepage_others:
path: /{_locale}/
controller: App\Controller\IndexController::homepage
requirements:
_locale: en-us|zh-cn|zh-hk

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

Dynamic routing file SF2

I am currently stuck on the multi-site management in Symfony2.
I have a Symfony2 project that allows multiple sites. Everything works fine but I block on managing files "routing".
My /app/config/routing.yml file I wish I could load the correct file routing.yml compared to the domain name:
project_routing:
resource: "%MY_DOMAIN%/routing.yml"
Unfortunately I can not put variable %my_domain% a file routing so I do not see how to fix this ...
The structure:
app/config/routing.yml is redirected to my %my_domain%
app/config/www.mywebsite.com/routing.yml
app/config/www.otherwebsite.com/routing.yml
So 1) base locale you can specify in app/parameters.yml
like this:
parameters:
locale: en
where locale: en default locale.
2) In default app/routing.yml file you should specify link for routing in your bundle like this:
fob_rental:
resource: "#fobRentalBundle/Resources/config/routing.yml"
prefix: /
3) In my src/fob/RentalBundle/Resources/config/routing.yml route looks like:
fob_offer_new:
pattern: /{_locale}/offers/new
defaults: { _controller: fobRentalBundle:Offer:new }
requirements:
_locale: en|spa

how to config the OneupUploaderBundle?

I am trying to config the OneupUploaderBundle. According to the documentations in
github, the given config is:
oneup_uploader:
mappings:
gallery:
frontend: blueimp # or any uploader you use in the frontend
For this config, I got this error:
Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "_uploader_upload_gallery" as such route does not exist.") in "MinnAdsBundle:Motors:oneup.html.twig" at line 25.
I tried to solve this issue by adding this config (as mentionned in github) to become like this:
oneup_uploader:
mappings:
gallery:
frontend: blueimp # or any uploader you use in the frontend
resource: .
type: uploader
The obtaind error is:
InvalidConfigurationException: Unrecognized options "resource, type" under "oneup_uploader"
So, I am wondering what I have missed in the config of this bundle.
For your information, The version I installed in my symfony project is:
"oneup/uploader-bundle": "1.3.1"
Thank for your help.
The documentation states the following:
To enable the dynamic routes, add the following to your routing configuration file.
# app/config/routing.yml
oneup_uploader:
resource: .
type: uploader
The correct place to put this is not the config.yml file but the routing.yml file instead.

How to display the 404 error page in Symfony2 dev environment

I want to work on the 404 page from the dev environment. I customize 404 using this file : app/Resources/TwigBundle/views/Exception/error.html.twig
This prod url work correctly : mysite.com/404
But this one mysite.com/app_dev.php/404 throw a NotFoundHttpException and give me the dev debug page.
Is it possible to display the error page instead of debug page ?
UPDATE:
The official documentation has now a chapter about that : Testing Error Pages during Development
To display error pages, in web/app_dev.php change second parameter to false
$kernel = new AppKernel('dev', false);
After testing what you need, change it back.
UPDATE
Thanks #user2019515 for pointing that out - now (2.3 and up) there's a link to WebfactoryExeptionsBundle in Symfony docs and the method I wrote above should not be used.
As of Symfony2.6 in dev environment, you can use the following route:
/_error/404.html
Where 404 is the error code to test and html the format of the request.
To be able to use this features, make sure you have the following entry in your routing_dev.yml file:
# app/config/routing_dev.yml
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
You need to override the exception_full.html.twig template on development.
app/Resources/TwigBundle/views/Exception/exception_full.html.twig
Symfony2 uses this template to provide you with as much debugging information as possible during development.
When the kernel is in debug mode, Symfony2 will use exception_full.html.twig, otherwise it will use the specific templates you override.
See vendor/symfony/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php, specifically the showAction() and findTemplate() functions for more details.
Also you can add routes to your routing_dev.yml file
error404:
path: /404
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error404.html.twig
error500:
path: /500
defaults:
_controller: FrameworkBundle:Template:template
template: TwigBundle:Exception:error500.html.twig

Resources