symfony2 special routes wildcard nothing else - symfony

problem:
need to have urls like this:
wall:
pattern: /{username}/
But.. it match every url like /Messages/, /Settings/
And only solution which comes to mind is to check if this user exists... and if no redirect to the real /Settings ? But its one select for each page and i dont like it.
Is there any other way? + i also probably need to denied register users with routes patterns? How?
AND NO I DONT WANT TO ADD TO URL SOMETHING LIKE /User/{username}

According to my comment, this could be a workaround:
pages:
pattern: /{page}/
requirements:
page: "(Messages|Settings|SomeOtherPage|YetAnotherPage)"
wall:
pattern: /{username}/
Basically, it requires that Page (Settings, Messages, etc) starts with uppercase ([A-Z]) and the rest of page name is not important.
As for the user-profile pages, they should start explicitly with lowercase character ([a-z]).
I don't know if this suffices your requirements...
EDIT:
This is in fact a bit simplified solution of #tomas.pecserke. I have just edited routing so all the pages are enumerated in single route.

I strongly recommend against this approach. You should definetly prefix the route. But if you must do it this way, you can specify other routes first. RouteMatcher chooses the first route that matches the pattern.
# src/Acme/DemoBundle/Resources/routing.yml
account:
pattern: /account
defaults: # ...
some_other_route:
pattern: /some_other_route
defaults: # ...
wall:
pattern: /{username}/
defaults: # ...
# you can set requirements
requirements:
username: [a-z0-9]+ # regular expression

Related

How to allow a "?" character in a route parameter in Symfony2?

I would like to know how to allow a "?" character in a route parameter in Symfony2. This is the route that I have (actually, the route parameter I am focusing on is "messtext"):
ikproj_groupe_sendreply1:
pattern: /{groupid}/{receiver}/{messtext}/{cible}/sendreply
defaults: { _controller: IkprojGroupeBundle:Messages:SendReply1 }
requirements:
messtext: .+
In fact, I had a look at this link: http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html which is about how to allow a "/" character in a route parameter in Symfony2. I did what I learnt from that tutorial exactly but the problem is that it doesn't work: Symfony2 still displays this error message:
No route found for "GET /groupe/sendreply/11/28/hello,%20dear%20sir...can%20I%20join%20your%20group"
So, my questions are: what is wrong in my code above and how can I resolve that?
Only 1 parameter of your route may accept '/' character. And this MUST be the last one as every following one are considered as part of the param value... In your case, Symfony routing never detect cible and sendreply parts of the URL.
Try to put messtext param at the very end of the URL :
ikproj_groupe_sendreply1:
pattern: /{groupid}/{receiver}/{cible}/sendreply/{messtext}
defaults: { _controller: IkprojGroupeBundle:Messages:SendReply1 }
requirements:
messtext: .+

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

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

Symfony2 access_control, Match all routes containing some word

The following only matches a path that starts with private:
access_control:
#...
- { path: ^/private, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
- { path: ^/private, roles: ROLE_NO_ACCESS }
How can I match all the routes that contain /private/ (and do not necessarily start with private)? For example, something that will match /message/private/blabla..
the equivalent of [path~="private"] with jQuery selectors.
Note: Does anyone know how we call the regex we are using here? Is this specific to yml or is this implemented by Symfony, I am confused.
Is there a chance I missed this in the doc somewhere, I mean the description with the specific matching patterns like start with, end with, contains etc....
^/ this pattern says everything that starts (^) with slash (/) and so on.
I suppose that /private or something like */private should work

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.

custom regex in route requirements

I'm trying to make a route parameter match an alphanumeric value that's 3 to 15 characters long. Code looks like this:
TestBundle_new:
pattern: /message/new/{name}
defaults: { _controller: TestBundle:Message:new }
requirements:
name: [a-zA-Z0-9]{2,15}
Unfortunately routing.yml fails to load. I was wondering if there is anything I can do to make this work?
You need to add quotes around your pattern.
name: "[a-zA-Z0-9]{2,15}"

Resources