duannuo7878 2019-07-09 15:11
浏览 59

如何根据域使用动态URL /路由?

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?