drls2738 2015-04-27 17:07
浏览 13
已采纳

具有不同区域的锂路线

I have a i18n route in Li3 that looks like:

Router::connect('/{:locale:[a-z]{2}/{:args}', [], [
    'continue' => true,
    'persist' => ['locale'],
]);

This way, when a user (or a crawler) enters my site with a language prefix, the locale is used to generate every link on the site.

For SEO purposes, I need to generate URLs in other locales such as:

GET /en/profile/john-doe
Canonical URL: https://www.example.com/en/profile/john-doe
Link hreflang for es: https://www.example.com/es/profile/john-doe
Link hreflang for pt: https://www.example.com/pt/profile/john-doe

My currency approach is cloning the current request, changing the locale, removing locale from the persist array, and using $request->to('url', ['absolute' => true]);.

But I can't get rid of the locale.

Any suggestions on how to address this?

  • 写回答

1条回答 默认 最新

  • doukun1450 2015-04-27 21:06
    关注

    I finally solved it extending HTML helper class:

    use lithium\template\helper\Html as BaseHtml;
    
    class Html extends BaseHtml
    {
        /**
         * Returns href lang link for a locale for the current request.
         *
         * @param string $locale
         * @return string <link />
         */
        public function getHrefLinkForLocale($locale)
        {
            return $this->link(
                'Canonical URL for ' . $locale,
                compact('locale') + $this->getRequest()->params,
                [
                    'absolute' => true,
                    'rel' => 'alternate',
                    'type' => 'alternate',
                    'hreflang' => $locale,
                ]
            );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏