【问题标题】:Zend multicountry redirectZend 多国重定向
【发布时间】:2023-03-20 04:48:01
【问题描述】:

目前我在默认位置有一个 Zend 应用程序 www.example.com/{controller}/{action}

但是当用户从特定国家访问时,如何检测他们的 IP 地址并将他们重定向到这个基于 countryCode 的 url www.example.com/uk/{controller}/{action}?

为了检测用户访问的国家/地区,我编写了一个助手:

require_once '../library/Ipinfo/ip2locationlite.class.php';

class Application_View_Helper_GetLocation extends Zend_View_Helper_Abstract
{
    public function getLocation()
    {

        $ipLite = new ip2location_lite;
        $ipLite->setKey('APIKEY');

        //Get errors and locations
        $locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
        $errors = $ipLite->getError();

        $country = strtolower($locations['countryName']);

        return "$country";
    }
}

以上代码将返回国家名称。如果用户从法国访问,我该如何重写 url 以使 url 变为 example.com/france/{controller}/{action}

【问题讨论】:

  • 您为什么认为该国家/地区表示语言偏好?应该使用 HTTP Accept-Language 标头。国家/地区代码对于特定国家/地区的内容很有用,例如货币和邮政编码表。
  • 另外,最好不要为每种语言设置单独的目录。您应该使用 ccTLD 或以Accept-Language 指定的正确语言返回内容,同时保持相同的 URL 方案。

标签: php zend-framework


【解决方案1】:

将您的视图助手重构为控制器插件并重定向。

控制器插件可以在请求调度循环的早期执行,因此您可以在加载和呈现任何控制器之前拦截请求并重定向到另一个响应。下面的示例(警告,可能包含错误!)

class App_Plugin_DetectCountry extends Zend_Controller_Plugin_Abstract {

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $ipLite = new ip2location_lite;
        $ipLite->setKey('APIKEY');

        //Get errors and locations
        $locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
        $errors = $ipLite->getError();

        $country = strtolower($locations['countryName']);

        //Check if set country equals detected country
        if (!isset($_COOKIE['country']) || $country !== $_COOKIE['country']) {
            $_COOKIE['country'] = $country;
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
            $redirector->gotoUrl($country . '/' . $request->getControllerName() . '/' . $request->getActionName()); 
        }
    }
}

【讨论】:

  • 我假设在这个例子中 $country 等于国家代码。否则,您需要提供国家到国家/地区代码的地图才能正确构建重定向 URL。
  • 嗨,是的,我会将国家代码映射到我从 ipinfodb 收到的国家/地区,例如,如果我收到新加坡,我会将其映射到 sg,因此 url 将是 example.com/sg/controller/action。谢谢
【解决方案2】:

我同意 @tripleee 关于使用 HTTP 标头而不是查找其 IP 的评论,这通常会导致错误的值,或强制远程代理后面的用户进入他们不想要的设置。

试试这个控制器插件,根据浏览器给定的用户区域设置进行重定向:

<?php

class Application_Plugin_Language extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        /*
        // if you add "localization.default_locale = "en_US" to your application.ini, uncomment the following
        $config = new Zend_Config($this->getOption('localization'), true);
        $loc = (isset($config->default_locale)) ? $config->default_locale : 'en_US';
        */

        $module = $request->getModuleName();
        if ($module != 'default') return ;

        // You can also check a cookie or session value here to see if you can return from the plugin as well

        $loc = 'en_US';

        Zend_Locale::setDefault($loc);

        try {
            $locale = new Zend_Locale(Zend_Locale::BROWSER);
        } catch (Zend_Locale_Exception $e) {
            $locale = new Zend_Locale($loc);
        }

        $language = $locale->getLanguage();  // e.g. "en", "de", "ru" etc.

        $urlHelper = new Zend_Controller_Action_Helper_Url();
        $url = $urlHelper->url(array('module' => $language, 'controller' => 'form', 'action' => 'index'));

        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
        $redirector->gotoUrl($url);
    }
}

如果用户当前正在请求默认模块,此插件将根据用户浏览器设置的语言重定向到具有语言名称的模块。

请注意,此代码不会检查您要重定向到的模块是否存在。您应该在重定向之前检查该语言是否受支持。

您还可以添加对包含用户所需语言的 cookie 或会话值的检查,并基于此进行重定向。

通过将其添加到application.ini来注册插件:

resources.frontController.plugins.language = "Application_Plugin_Language"

如果您想根据国家而不是语言进行重定向,请将$language = $locale-&gt;getLanguage(); 更改为$region = $locale-&gt;getRegion();

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多