Hash escape in URL (routing.yml/path) Symfony 2 - symfony

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

Related

How do I know what key/value and the proper syntax of my router.yaml file in symfony?

I am just learning symphony please bear with me. I do not want to blindly copy and paste stuff without a real meaning.
In routes.yaml i am seing string such as this :
route1:
path: /foo
defaults: { _controller: 'MyController::fooAction' }
why does "_controller" use the underscore? how would i know that it need the underscore without saying : "that is just the way to use it" ?
why are they using :: to refer to a function method that is not a static method?
How do i get a list of all the keys/value syntax for that that router file?
where do i get the hierarchy of the key/value pair of the yaml file?
In other places i am seeing string like this:
app_lucky_number:
path: /lucky/number
controller: App\Controller\LuckyController::number
This one looks cleaner but where is the underscore in word "controller"?
Why so many inconsistencies?
BTW: I have read this: BTW: I read this: http://symfony.com/doc/current/components/routing.html#defining-routes
it is stated that a full route has seven parts but how do i know the proper syntax of those parts?
Any insights will be appreciated...
Thanks.

Remove Symfony2 API URL trailing slash

I'm starting to design an API with Symfony2, and I have a problem with the trailing slashes on routes.
For example, let's say I have a Person entity, prefixed with /person in the routing.yml file:
api.persons:
resource: "#AppBundle/Controller/PersonsController.php"
type: annotation
prefix: /persons
And in the annotations of the controller:
#Route("/", methods="{GET}", name="persons_get"))
The result of Symfony2 command router:debug will give me this URL : /persons/. I want to remove this trailing slash. Do you have an idea ?
I got this issue too some time ago and while looking for a solution I find this: https://github.com/symfony/symfony/issues/1972
I agree with Fabien (fabpot), since prefixes are used in the same way as directories, you are forced to set a string that will be appended to the prefix, and using an empty route is a nonsense.
If you do not want trailing slashes then define it as:
#Route("/persons", methods="{GET}", name="persons_get")
Use prefixes just for composition purposes.

Translated Symfony routes with multiple parameters

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

Symfony2 - encoding url parameters like slash, dot and plus

Ich have a problem with symfony2(2.0.16) routing.
I try to run 2 routes like
route1:
host/my/route/{param}
requirements:
param: ".*[^/]$"
route2:
host/my/route/category/{param}
requirements:
param: ".*[^/]$"
As you can imagine, the second route will not be called, not matter what..
what i actually want to do is a search and a specialized search, therefore i need to allow also dots, plus and slashes..
I tried to encode the slash (urlencode to %2F or %252F) so that i can change the requirement, but symfony always decodes it before the routing, so i get an routing error if i remove the requirement.
I thought about to use base64 encoding, but that cant be the solution to my problem..
EDIT: i can also not rely on the order of the routes, because i import the routes from many diffrent bundles..
#This one before the other to be considered !
route2:
host/my/route/category/{param}
requirements:
param: ".+"
route1:
host/my/route/{param}
requirements:
param: ".+"
The parameter for your first route can't be category else it will match it instead of ignoring it. You can change the regex to ignore this value:
route1:
host/my/route/{param}
requirements:
param: "(?!category/).*[^/]$"
route2:
host/my/route/category/{param}
requirements:
param: ".*[^/]$"
Now a parameter called category followed by a slash will be ignored and the pattern won't match, except by the second route.

ASP.NET routing: Literal sub-segment between tokens, and route values with a character from the literal sub-segment

The reason I'm asking is because IIS protects certain ASP.NET folders, like Bin, App_Data, App_Code, etc. Even if the URL does not map to an actual file system folder IIS rejects a URL with a path segment equal to one of the mentioned names.
This means I cannot have a route like this:
{controller}/{action}/{id}
... where id can be any string e.g.
Catalog/Product/Bin
So, instead of disabling this security measure I'm willing to change the route, using a suffix before the id, like these:
{controller}/{action}_{id} // e.g. Catalog/Product_Bin
{controller}/{action}/_{id} // e.g. Catalog/Product/_Bin
But these routes won't work if the id contains the new delimeter, _ in this case, e.g.
// These URL won't work (I get 404 response)
Catalog/Product_Bin_
Catalog/Product/_Bin_
Catalog/Product/__Bin
Why? I don't know, looks like a bug to me. How can I make these routes work, where id can be any string?
Ok, I have a definitive answer. Yes, this is a bug. However, at this point I regret to say we have no plans to fix it for a couple of reasons:
It's a breaking change and could be a very hard to notice one at that.
There's an easy workaround.
What you can do is change the URL to not have the underscore:
{controller}/{action}/_{id}
Then add a route constraint that requires that the ID parameter starts with an underscore character.
Then within your action method you trim off the underscore prefix from the id parameter. You could even write an action filter to do this for you if you liked. Sorry for the inconvenience.
You can use characters that are not allowed for a directory or file name like: *,?,:,",<,>,|.
With ASP.NET MVC if you look at the source they have a hard-coded value for the path separator (/) and to my knowledge cannot be changed.

Resources