I have an e-commerce website that is available in multiple languages through multiple domains. For example: example.com (english), example.fr (french), and so on...
I want to have dynamic URLs like so: example.com/product/shoes-54
, example.com/produit/chaussures-54
.
How to declare those routes depending of the domain knowing that /produit must be accessible only on example.fr
and /product
only on example.com
?
I've tried to declare those routes in a switch case depending of domain.
<?php
$domainName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
switch($domainName) {
case 'example.fr':
case 'www.example.fr':
$routes->connect('/ajoutez-votre-experience', ['controller' => 'Home', 'action' => 'add']);
$routes->connect('/contact', ['controller' => 'Home', 'action' => 'contact']);
$routes->connect('/conditions-d-annulation', ['controller' => 'Home', 'action' => 'refund']);
$routes->connect('/hote/:slug', ['controller' => 'Host', 'action' => 'index'])->setPass(['slug']);
$routes->connect('/experience/:slug', ['controller' => 'Experience', 'action' => 'index'])->setPass(['slug']);
$routes->connect('/region/:region', ['controller' => 'Location', 'action' => 'index'])->setPass(['region']);
$routes->connect('/region/:region/:region_sub', ['controller' => 'Location', 'action' => 'index'])->setPass(['region','region_sub']);
$routes->connect('/region/:region/:region_sub/:city', ['controller' => 'Location', 'action' => 'index'])->setPass(['region','region_sub','city']);
$routes->connect('/recherche', ['controller' => 'Search', 'action' => 'index']);
break;
case 'example.com':
case 'www.example.com':
case '':
default:
$routes->connect('/add-your-business', ['controller' => 'Home', 'action' => 'add']);
$routes->connect('/contact', ['controller' => 'Home', 'action' => 'contact']);
$routes->connect('/cancellation-condition', ['controller' => 'Home', 'action' => 'refund']);
$routes->connect('/host/:slug', ['controller' => 'Host', 'action' => 'index'])->setPass(['slug']);
$routes->connect('/experience/:slug', ['controller' => 'Experience', 'action' => 'index'])->setPass(['slug']);
$routes->connect('/location/:region', ['controller' => 'Location', 'action' => 'index'])->setPass(['region']);
$routes->connect('/location/:region/:region_sub', ['controller' => 'Location', 'action' => 'index'])->setPass(['region','region_sub']);
$routes->connect('/location/:region/:region_sub/:city', ['controller' => 'Location', 'action' => 'index'])->setPass(['region','region_sub','city']);
$routes->connect('/search', ['controller' => 'Search', 'action' => 'index']);
break;
}
It's working fine except when I need to get specific URL of a product in my CRON tasks. Since I'm not on a specific domain, I can't "guess" the URL and so the route.
I thought about naming my routes, but I means I need to declare all of theme to get it in my CRON task, which doesn't suit me because I don't want french website to be accessible on example.fr/product/shoes-54
.
What's the best way to handle multiple domains/langs routes in CakePHP application ? I can't find enough documentation on that unfortunately...