legacy redirection with symfony routing bug? - symfony

I would like to redirect (301) all the requests like "/cgi/.*" to a "parking" page of my symfony (3.4) application "/park".
I've created a route like this :
old_legacy:
path: /cgi/{catchall}
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /park
permanets: true
requirements:
catchall: ".*"
Everything is ok when urls are "/cgi/foo/bar", "cgi/bar.php?x=2", etc., but it doesn't work when the URL end with "index.php" : "/cgi/index.php", "cgi/foo/index.php?x=2".
I goes in symfony code (same for SF master) and I found something wrong for my case:
To get baseUrl, the HttpFoundation/Request object try to get the php script filename "SCRIPT_FILENAME", but if the basename is the same as SCRIPT_NAME, it get this one !
Ok, but SCRIPT_FILENAME is "/var/www/public/index.php" and SCRIPT_NAME is "/cgi/index.php", and both basename are "index.php", thus, base URL is calculated with "/cgi/index.php" which is wrong, and the base URL become "null".
At the end of the story, the router look for the path "/" and not "/cgi/index.php".
Do other people have this issue ?

Related

Symfony 3.4. Not any route is working - No route found for "GET /

My Symfony 3.4 project does not recognize any route anymore. Debugging routes in terminal (php bin/console debug:router) does show all the routes but app_dev.php keeps giving error messages. Routing is enabled and set correct in routing.yml file.
Clearing cache also did not work.
mail_chimp:
resource: "#MailChimpBundle/Controller/"
type: annotation
prefix: /
/**
* #Route("/klanten/lijst")
*/
public function klantenLijstAction() {
return;
}
screenshot
Thanks.
> the routing tries to match routes one by one in the order of their definitions; whenever one matches, work is done.
In the routes-list image, you look to have three (maybe more) routes to answer the URL '/'. Which single one should run a controller action?
You should probably set prefixes for the URLs that aren't the index route.

Symfony 3 routes + VueJS's index.html with F5 falls to 404

I try connect Vue.js with Symfony 3, but have no idea how to create proper route configuration for it.
Project tree looks like that:
Project root
- app/
- Resources/
- views/
- default/
- web/
- assets/
- static/
index.html // here Vue is included
From Symfony I provide some REST API, the routes to it are prefixed by /api/. And I have some routes in Vue SPA.
The problem is if I press F5 in browser on any Vue route, which of course is not included in my routing.yml, browser returns 404 error from Symfony.
One half-working decision is when I put content of index.html to Resourses/views/default/twig with such a route:
fallback_for_vue:
path: /{req}
defaults: { _controller: 'AppBundle:Default:index' }
requirements:
req: ".+"
,taken from another question, but this is not actually what I need, because I don't want my frontend programmer to go somewhere except web/ folder.
So which configuration of Symfony routes I should use?
You need a wildcard address to point to your Vue.js application. Otherwise when you reload you send a request to Symfony, and it doesn't know hoy to handle it.
vuejs_wildcard_route:
defaults: { _controller: AppBundle:YourController:yourAction }
path: /vuejsappbasepath/{whatever}
requirements:
whatever: .*
defaults:
whatever: null
I have this setup working with a reactjs app and is working perfectly.
YourController is the controller that contains yourAction, the one that loads the template with the Vue.js panel.

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 route not found order

I built a symfony2 project and I have a problem with my app/config/routing.yml. I have two routes for two bundles and only the last route can be resolved. If I change the order, still only last route can be resolved and for the first route I have an error message "No route found for "GET /collaborateur/". Any solutions?
This is my app/config/routing.yml:
front_office:
resource: "#FrontOfficeBundle/Resources/config/routing.yml"
prefix: /collaborateur
back_office:
resource: "#BackOfficeBundle/Resources/config/routing.yml"
prefix: /platform
First, it would be good to run router:debug in order to see which routes got loaded in fact.
php app/console router:debug
You should be able to see both of those here.
Secondly, I noticed that you sent a request to GET /collaborateur/. You might be hitting a known Symfony2 routing problem when it comes to trailing slash. It was described in this cookbook.
Can you try to open /collaborateur (without trailing slash)?

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