I'm in the process of upgrading from Symfony 2.0 to 2.3. We have routes with hashes defined since we have a single page app.
A route configured via annotations:
/**
* #Route("/app#orders/{id}", name="app_order")
*/
We use Twig to generate emails and use these routes within the Twig templates:
View order
Before upgrading, this worked fine. After the upgrade, the # gets encoded to %23 but the slashes remain intact. This of course generates an invalid URL in the email.
Why are only the hashes encoding and not the slashes? It should be all or nothing. What options do I have here other than doing a string replace?
Things I've already tried doing which don't help:
Setting autoescape to false {% autoescape false %}
Using raw {{ url(...)|raw }}
Using raw and autoescape=false in conjunction
If you take a look at the UrlGenerator code, you can see, the hashtag is not decoded after rawurlencode.
The escaping of the hashtag was added in commit 6039569.
As a workaround you could extend the UrlGenerator class and replace the $decodedChars array with hashtag included. Then tell Symfony to use your generator class:
parameters:
router.options.generator_base_class: Acme\MyBundle\Routing\HashtagDecodedUrlGenerator
Related
Someone can explain this?
In twig, I need to detect if certain controller is loaded. Depending on it, it should display a HTML block or another.
I have this comparison in twig template:
{% if app.request.attributes.get("_controller") == 'App\Controller\DefaultController::index' %}
Even when controller and action is actually "App\Controller\DefaultController::index" expression evaluates to false showing the block intended to be shown when the controller is other.
Besides explanation, I need to solution please.
by the way, when I print the controller name, using
{{ app.request.attributes.get("_controller") }}
I can check this strange behaviour.
Regards
Jaime
Twig is reading the backslashes in your string as escape characters. To make it work you must escape the backslashes. Like this:
'App\\Controller\\DefaultController::index'
I am trying to get along with translation service in symfony2. The problem I am facing is that this code always returns the english version of the key
$translator = $this->get('translator');
$translator->setlocale('fr'); // locale gets ignored
die($translator->trans('pisica')); //always outputs cat altough the translation in french exists too.
I would like to specify in controller witch locale I am using because it is best for what I am trying to acomplish
Set the Locale in the Request object, not in the translator service.
$request->setLocale('fr');
$translator->trans('something');
As the title suggests, I'm using Symfony in conjunction with the JMSTranslationBundle and JMSI18nBundle in order to serve translated routes.
Here's my currently configured route:
/{location}/{profession}/{specialty}
So the route
/berlin/arzt/allgemein
is successfully pushed to the correct controller and action.
The JMSI18nBundle is automagically prefixing my English routes with /en/. This works for every other route with a non-dynamic component (such as /profile/{slug}/). This DOES NOT work, however, when using the English version of the above example. i.e.
/en/berlin/doctor/general
I'm guessing the router is not reading this properly as the English version of the normal route, and instead tries to assign location = en, profession = berlin, etc, which is obviously incorrect.
I've tried defining optional parameters, more complicated regexes, and trailing slashes for the translation (all with cache flushes in between). None of this works. What DOES work, is inserting a pointless non-dynamic component, i.e. /en/s/berlin/doctor/general etc
As a part of the business requirements, we don't want this additional pointless non-dynamic URL component.
So, my question is: how can I use (prefixed) translatable URLs in Symfony that contain nothing but dynamic fields?
Your help is greatly appreciated!
Solved:
As is the norm with Friday-afternoon problems, I found I had a $ inside my translated route rule, like so:
/{location}/{$profession}/{specialty}
Removing it and flushing the cache resulted in the route working.
tl;dr - PEBKAC
I have problem with urls in my Symfony 2.3 aplication.
I have defined routing like this:
home_how_to_buy:
path: /strefa-wiedzy#jak-kupic
defaults:
_controller: FrameworkBundle:Template:template
template: 'GLHomeBundle:Default:faq.html.twig'
The problem is that when I create links for this page I have something like:
app_dev.php/strefa-wiedzy%23jak-kupic
I have been looking for escaping in yml files, but none of those solutions work for my path.
I will be gratefull for any help.
As stated in my answer here the hashtag is not intended to be in symfony routing. You can do the suggested workaround. But first you should consider, do you really need url-fragments in routing?
PHP's rawurlencode() encodes all chars except a-zA-Z0-9-._~ according to RFC 3986. But we want to allow some chars to be used in their literal form (reasons below). Other chars inside the path must of course be encoded, e.g. ? and # (would be interpreted wrongly as query and fragment identifier), ' and " (are used as delimiters in HTML).
I am wondering if it is possible to define routes in FOSRestBundle methods that specify the format with something other than a "dot" preceding them.
For example, suppose I'd like the following to work:
http://somesite.com/api/user/20 (renders in the default/fallback format)
http://somesite.com/api/user/20/json (renders in JSON)
http://somesite.com/api/user/20/xml (renders in XML)
If I attempt to define a route like:
/**
* #Get("/user/{id}/{_format}
*/
public function getUserAction($id)
{
// do stuff
}
I get this:
Route pattern "/api/users/{maximum}/{_format}.{_format}" cannot reference variable name "_format" more than once.
That made me realize that it -- and by it, I assume we are talking FOSRestBundle and not Symfony2 by default -- is automatically adding the ".{_format}" to the end of whatever route I define. I was surprised!
So right now, in my earlier example, it works as follows:
http://somesite.com/api/user/20 (renders in the default/fallback format)
http://somesite.com/api/user/20.json (renders in JSON)
http://somesite.com/api/user/20.xml (renders in XML)
A small difference to be sure, but, I am trying to port over a legacy app that uses this syntax. Is what I am trying to do possible? If so, how can I disable that automatic addition of ".{_format}" to each route?
Thanks!
Over a year has passed and FOSRestBundle now supports the feature I was looking for above.
You can control this behavior via the following configuration:
fos_rest:
routing_loader:
default_format: ~
include_format: true
Set the include_format to false to remove the format from the route.
You can view the expanded configuration options for FOSRestBundle here.