Multiple routes with multiple domains - symfony

Let's say I've got a website with multiple (sub)domains:
acme.com (USA)
acme.nl (Netherlands)
be.acme.eu (Belgium)
de.acme.eu (Germany)
fr.acme.eu (France)
etc...
I thought this should be very simple to configure, so I made this routing.yml:
usa:
host: "acme.com"
resource: "#WebsiteBundle/Controller/"
type: annotation
defaults:
country: "en"
netherlands:
host: "acme.nl"
resource: "#WebsiteBundle/Controller/"
type: annotation
defaults:
country: "nl"
europe:
host: "{country}.acme.eu"
resource: "#WebsiteBundle/Controller/"
type: annotation
But if I run router:debug, only the last route (in this case {country}.acme.eu) shows up. If I change to order, the last option shows up.
How can I use different (sub)domains for all my countries?

This is because all the routes point to one resource. Every later route will override routes defined before.
But you can use another solution:
main_route:
host: "{country}.acme.{domain}"
resource: "#WebsiteBundle/Controller/"
type: annotation
defaults:
country: "en"
Then check in some listener before controller for valid url and process parameters.

You should use Symfony's hostname routing.
So your routes would look like:
international_homepages:
path: /
host: "{subdomain}.acme.{extension}"
defaults:
_controller: AcmeBundle:Main:defaultHomepage
subdomain: www
extension: com

Related

Symfony redirects - routers.yaml before annotations.yaml

I'm using annotations in my project but to redirects using routes.yaml
If exists routing (ex. /home name="home"} that redirect not working:
home-301:
path: /home
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /new-home
permanent: true
I found solution but I thing it is no good practice:
cleared: config/routes/annotations.yaml
and appended to the end of the file config/routes.yaml:
controllers:
resource: ../../src/Controller/
type: annotation
how to do it right?
My solution:
all redirects put in: config/routers/redirects.yaml
and config/routes.yaml:
redirects:
resource: routes/redirects.yaml
annotations:
resource: routes/annotations.yaml

How use requirements for variable in symfony route

I need to add a requirement for my api route (only allow 3 mgn , user and adm) but i get the error:
MissingMandatoryParametersException
Some mandatory parameters are missing ("role") to generate a URL for route "api_doc".
here is my route:
api_platform:
resource: .
type: api_platform
prefix: /{role}/api
requirements:
role: "mng|adm|user"
should i install or set something?, thank you
already find a solution
if some person need it
api_platform:
resource: .
type: api_platform
prefix: /{app}/api
defaults:
app: take|mngr|drvr
requirements:
app: take|mngr|drvr
thank you

In Symfony is it possible to if a route matched, forward to other route

I have two routes in two separated bundles: bundleA_route, bundleB_route. In my /app/config/routing.yml I load them as resources like:
bundle_a_routing:
resource: "#SomeABundle/Resources/config/routing.yml"
prefix: /
bundle_b_routing:
resource: "#SomeBBundle/Resources/config/routing.yml"
prefix: /
In most cases this sequence is required, first of all try to match on routes in bundle_a_routing, and then try to match on routes in bundle_b_routing, but in only one case I want to make an exception, I want a single route defined in bundle_b_routing to be matched before the more "concessive" route defined in bundle_a_routing:
route_in_bundle_a:
path: /admin/{path}
defaults: { _controller: SomeABundle:SomeCtrl:someAction }
methods: [GET]
requirements:
path: ^(.*)$
route_in_bundle_b:
path: /admin/download/{formId}/{fileName}
defaults: { _controller: SomeBBundle:SomeOtherCtrl:someOtherAction }
methods: [GET]
requirements:
formId: \d+
fileName: ^([a-fA-F0-9]{32}(?:\.[a-zA-Z0-9]{1,222})?)$
Now the request URI "http://servername/web/app_dev.php/admin/download/12/23d2fff7f606e93acac9ede5b4e2b394.png" matches the first, but I want to match the second...what is the official scenario in cases like this?
You should change the order in which you are importing routes, so the route http://servername/web/app_dev.php/admin/download/12/23d2fff7f606e93acac9ede5b4e2b394.png will trigger the route_in_bundle_b first and the routes that doesn't have the download segment will not trigger it and will trigger the route_in_bundle_a rule.

In Symfony 2, how do I handle routing for multiple domains?

I have my app setup to use 3 subdomains, I also have 2 domains that point to the same app, giving me this set of FQDNs
admin.domain1.com, admin.domain2.com
www.domain1.com, www.domain2.com
kiosk.domain1.com, kiosk.domain2.com
In routing.yml I can set up the host / resource keys to point those domains at the relevant bundle, but I can only do it for one of the domains at a time.
incompass_admin:
host: admin.domain1.com
resource: "#IncompassAdminBundle/Resources/config/routes.yml"
incompass_web:
host: www.domain1.com
resource: "#IncompassWebBundle/Resources/config/routes.yml"
incompass_kiosk:
host: kiosk.domain1.com
resource: "#IncompassKioskBundle/Resources/config/routes.yml"
When I try something like this
incompass_kiosk_1:
host: kiosk.domain1.com
resource: "#IncompassKioskBundle/Resources/config/routes.yml"
incompass_kiosk_2:
host: kiosk.domain2.com
resource: "#IncompassKioskBundle/Resources/config/routes.yml"
The second domain is the only one that is recognised, kiosk.domain1.com throws an exception: No route found for "GET /"
Is there a way to either set the domain as a wildcard in routing.yml, or to point 2 different routes to the same resource?
edit: I've also tried to have
IncompassKioskBundle/Resources/config/routes_1.yml
IncompassKioskBundle/Resources/config/routes_2.yml
and
IncompassKioskBundle/Resources/config/routes.yml
IncompassRoutingBundle/Resources/config/kiosk_routes.yml
where the names of the routes in the 2 files are different e.g.
// routes.yml
domain_one_kiosk_scanning:
type: annotation
prefix: /
resource: Incompass\KioskBundle\Controller\ScanningController
domain_one_kiosk_printing:
type: annotation
prefix: /print
resource: Incompass\KioskBundle\Controller\PrintingController
// kiosk_routes.yml
domain_two_kiosk_scanning:
type: annotation
prefix: /
resource: Incompass\KioskBundle\Controller\ScanningController
domain_two_kiosk_printing:
type: annotation
prefix: /print
resource: Incompass\KioskBundle\Controller\PrintingController
That did not work.
You can use placeholders in routes, so try something like this:
incompass_kiosk:
host: "kiosk.{domain}.com"
resource: "#IncompassKioskBundle/Resources/config/routes.yml"
requirements:
domain: domain1|domain2
defaults: { domain: domain1 }
You could alternatively handle this in your .htaccess or virtual host configuration.

Symfony2 route based on host. With parameters

I have followed the instructions here:
http://symfony.com/doc/current/components/routing/hostname_pattern.html
To make the route based on the host. However I want to use parameters instead of hard coding. The documentation says you can use service parameters, but I seem to be having trouble getting the parameters to work.
Here is the code from routing.yml:
rc_course_new:
pattern: /course/new
host: "{ domain }"
defaults: { _controller: CoursesRCWizardBundle:Wizard:new }
requirements:
domain: "%rc_domain%"
And here is the code from services.yml:
parameters:
rc_domain: my.domain.com
I get this error (looks like it is not picking up the parameter but seeing it as a hard code):
Oops! Google Chrome could not find { domain }
Managed to fix this:
In the routing:
rc_course_new:
pattern: /course/new
host: "%rc_domain%"
defaults: { _controller: CoursesRCWizardBundle:Wizard:new }
In the services (might work better in the parameters file) file:
parameters:
rc_domain: my.domain.com

Resources