Zend multicountry redirect

I currently have a default Zend app www.example.com/{controller}/{action}

but when a user visits a specific country, how to determine their IP address and redirect them to that URL based on countryCode www.example.com/uk/{controller}/{action}?

To determine the country from which the user is visiting, I wrote an assistant:

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";
    }
}

The above code will return the name of the country. If the user is visiting from France, how can I rewrite the url so that the url becomes example.com/france/{controller}/{action}?

+5
source share
2 answers

Restore your view assistant in the controller plugin and redirect it.

, , - . (, !)

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()); 
        }
    }
}
+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->getLanguage(); $region = $locale->getRegion();

, .

+1

All Articles