Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend多国重定向_Php_Zend Framework - Fatal编程技术网

Php Zend多国重定向

Php Zend多国重定向,php,zend-framework,Php,Zend Framework,目前我在默认位置有一个Zend应用程序 www.example.com/{controller}/{action} 但是,当用户从特定国家访问时,如何检测他们的ip地址并将其重定向到基于国家代码的url www.example.com/uk/{controller}/{action} 为了检测用户访问的国家,我写了一个助手: require_once '../library/Ipinfo/ip2locationlite.class.php'; class Application_View_He

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

但是,当用户从特定国家访问时,如何检测他们的ip地址并将其重定向到基于国家代码的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}

将视图帮助程序重构为控制器插件并重定向

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


我同意@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);
    }
}
如果要基于国家而不是语言重定向,请更改
$language=$locale->getLanguage()
$region=$locale->getRegion()


希望这能有所帮助。

为什么你认为这个国家表示有语言偏好?应该使用HTTP
Accept Language
头进行此操作。国家代码对于特定于国家的内容很有用,比如货币和邮政编码表。另外,最好不要为每种语言设置单独的目录。您应该使用cctld,或者返回由
Accept language
指定的正确语言的内容,同时保持相同的URL方案。在本例中,我假设$country等于国家代码。否则,您需要提供一个国家到国家代码的地图,以正确构建重定向URL。您好,是的,我将把一个国家代码映射到我从ipinfodb收到的国家,例如,如果我收到新加坡,我将把它映射到sg,因此URL将是example.com/sg/controller/action。谢谢
<?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);
    }
}
resources.frontController.plugins.language = "Application_Plugin_Language"