Use Symfony expression to populate default route parameters - symfony

I have route like this:
os_user_security_check:
pattern: /login_check
host: "{subdomain}.%base_host%"
defaults:
_controller: FOSUserBundle:Security:check
subdomain: "expr(request.get('subdomain'))"
requirements:
_method: POST
subdomain: "[a-z0-9\-_]+?"
What I want, is by default populate subdomain by default to prevent to affect it when generating the route, how ?

Related

Can't override homepage trailing slash

My site have no "homepage" that use the base url (mydomain.com)
The default routing include the _locale parameter, and thus, fail to match one part of the route depending on how I set it.
Config A
homepage:
path: /{_locale}/
defaults:
_locale: '%locale%'
requirements:
_locale: '%locales%'
This first config will match the route homepage with the following URLs
mydomain.com/en
mydomain.com/en/
But will fail to match
mydomain.com
mydomain.com/
Config B
homepage:
path: /{_locale}
defaults:
_locale: '%locale%'
requirements:
_locale: '%locales%'
This second config will match the route homepage with the following URLs
mydomain.com
mydomain.com/
mydomain.com/en
But will fail to match
mydomain.com/en/
Any idea what I'm missing in my config to match the 4 possibles URL patterns?
Found my answer:
homepage:
path: /{_locale}{trailingSlash}
defaults:
_locale: '%locale%'
trailingSlash: ''
requirements:
_locale: '%locales%'
trailingSlash: /?

Locale as prefix and global for the whole site

I'm trying to define the routes of my site with {_locale} as a prefix so there will be routes like:
mysite.com/about-us
mysite.com/es/about-us
mysite.com/en/about-us
My first problem is that defining {_locale} as prefix makes it mandatory and the route mysite.com/about-us won't work. Currently defined this way:
#Acme/WebBundle/Resources/config/routing.yml
app_about_us:
path: /{_locale}/about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs, _locale: es}
requirements:
_locale: es|en
#app/config/routing.yml
Acme_web:
resource: "#AcmeWebBundle/Resources/config/routing.yml"
prefix: /
Also, I don't want my routes to display the {_locale}, so that if anyone types:
mysite.com/es/about-us it changes to *mysite.com
Finally if there is any way to set the prefix globally for all the site instead of putting it on every route would be awesome.
OK, first of all you can set default locale for a whole application
# app/config/config.yml
framework:
default_locale: en
Second - doing it this way, you can declare two routes:
app_about_us:
path: /{_locale}/about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs}
requirements:
_locale: es|en
app_about_us_locale:
path: /about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs, _locale: es}
Im not sure is it possible to do it different way without additional bundles, events etc...
Other way
You can set session locale - here is example to make locale sticky.
This way you can create normal routing, without params. So how set new session locale? Create special routing to setting new locale and redirect to your original page.
app_set_locale:
path: /{_locale}/
defaults: { _controller: AcmeWebBundle:Locale:setLocale}
inside controller set locale and redirect to previous page
Read Symfony documentation about Routing, Locale, Translate - maybe you will found something interesting :)

Symfony Match Route with Multiple Hosts

I know it is possible to filter a route by host, like this:
irc_backend.report.stacking_issue:
path: /reports/stacking-issues
host: backend.domain.com
defaults: { _controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index }
Is it possible to alter this configuration to match multiple domains? For example:
irc_backend.report.stacking_issue:
path: /reports/stacking-issues
host: backend.domain.com|dev.backend.domain.com
defaults: { _controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index }
I'm trying to avoid setting up 2 routes for every page.
You can try placeholders in your hostname with requirements (i.e.. Symfony Documentation)
irc_backend.report.stacking_issue:
path: /reports/stacking-issues
host: "{mydomaines}"
defaults: { _controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index }
requirements:
mydomaines: backend.domain.com|dev.backend.domain.com
If you read the Symfony documentation, you'd know that you need to use placeholders. For your code, it would be :
irc_backend.report.stacking_issue:
path: /reports/stacking-issues
host: "{subdomain}.domain.com"
defaults:
_controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index
subdomain: backend
requirements:
subdomain: backend|dev.backend
I hope it'll help you !
EDIT :
If you want your default subdomain to be the current one, you can use a parameter like this :
irc_backend.report.stacking_issue:
path: /reports/stacking-issues
host: "{subdomain}.domain.com"
defaults:
_controller: IRCBackendBundle:Reports/Product/StackingIssueReport:index
subdomain: "%subdomain%"
requirements:
subdomain: backend|dev.backend
Then you will be able to define this parameter in an Event listener with this line of code :
$container->setParameter("subdomain", $your_subdomain);
P-S : Don't forget to add the Service container to your listener's dependencies

Symfony 2 - can't configure application for hosts

I have problem with setting hosts for my symfony application. For now we have one domain, let's sey it was domain.pl for all four languages. So rule in YML was:
front_common:
resource: "#FrontCommonBundle/Resources/config/routing.yml"
prefix: /{_locale}
defaults: { _locale: pl }
requirements:
_locale: "[a-z]{2}"
front_common_locale:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
_locale: pl
path: /pl
permanent: true
So if someone enters domain.pl, he/she will be redirected to domain.pl/pl. Other pages was domain.pl/en, domain.pl/de and so on...
Now we get another domain, let's say domain.eu, that should show only english version, so domain.eu show english, others still redirects to /pl /de etc. My routing now looks this way:
front_common_eu:
host: domain.eu
resource: "#FrontCommonBundle/Resources/config/routing.yml"
prefix: /
defaults: { _locale: en }
front_common:
resource: "#FrontCommonBundle/Resources/config/routing.yml"
prefix: /{_locale}
defaults: { _locale: pl }
requirements:
_locale: "[a-z]{2}"
front_common_locale:
path: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
_locale: pl
path: /pl
permanent: true
When I enter domain.pl it works fine, and redirects me to /pl. But when I enter domain.eu it works the same, it redirects me to /pl. If I remove fron_common_locale route, I've got 404. Help, what am I doing wrong?
PS. According to this: Routing prefix as follows the Virtual Host it should work...
Replace _locale on front_common_locale with:
_locale: pl|en

Symfony2 Routing with Folders (containing multiple forward slashes)

I am looking to setup some routes in Symfony2, but I am struggling to setup a dynamic route for folders.
I am trying to setup a route that accepts the following: /department/sub-department/sub-sub-department/product-url.html
From that route all I need is the product-url and the rest is more for SEO. The problem I have is that a route may have many department levels in the URL, so I need to ignore everything before the product-url.
It seems like the "/" is the problem here, so is there a way to escape the slashes.
If I don't use any of the departments in the routing I can use this:
product:
pattern: /{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
So, I basically need something like this:
product:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
Where the {department} can be one or more departments with forward slashes in.
Is that possible at all?
There's a nice article about it in the cookbook:
You must explicitly allow / to be part of your parameter by specifying a more permissive regex pattern.
In your case the route definition would have to be
product:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
requirements:
department: ".+"
product:
pattern: /{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest, department: ~ }
product_department:
pattern: /{department}/{url}.html
defaults: { _controller: CompanyBundle:System:pageRequest }
requirements:
department: '[\w\d\/\-]+'

Resources