PHP code example of andersundsehr / geo_redirect

1. Go to this page and download the library: Download andersundsehr/geo_redirect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andersundsehr / geo_redirect example snippets


class MyClass {
    // inject the IpCountryLocatorInterface using constructor DI
    public __construct(
        protected readonly IpCountryLocatorInterface $ipCountryLocator
    ) {
    }
    
    // retrieve the country from the request
    public function getIpCountry(): ?string {
        return $this->ipCountryLocator->getIpCountry();
    }
}

$siteLanguage = GeneralUtility::makeInstance(SiteLanguageFinderService::class)->findByRequest($request);
// or if you don't have a request object: (in cli the request object is nguage



declare(strict_types=1);

namespace AUS\GeoRedirect\EventListener;

final class BeforeSiteLanguageFinderEventListener
{
    public function __invoke(\AUS\GeoRedirect\Dto\BeforeSiteLanguageFinderEvent $event): void
    {
        $languageId = $this->customLanguageIdFinder($event);
        if ($languageId) {
            $event->siteLanguage = $event->site->getLanguageById($languageId);
        }
    }
    // ...
}



declare(strict_types=1);

namespace AUS\GeoRedirect\EventListener;

final class BeforeSiteLanguageFinderEventListener
{
    public function __invoke(\AUS\GeoRedirect\Dto\CollectIpCountryLocatorEvent $event): void
    {
        $event->addFirst(\AUS\GeoRedirect\IpCountryLocator\MyCustomIpCountryLocator::class);
        // or
        $event->addLast(\AUS\GeoRedirect\IpCountryLocator\MyCustomIpCountryLocator::class);
        // or if you want to add it at a specific position:
        $list = $event->getLocatorClasses();
        // manipulate the list as you like
        $event->setLocatorClasses($list);
    }
}