PHP code example of terminal42 / contao-geoip2-country

1. Go to this page and download the library: Download terminal42/contao-geoip2-country 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/ */

    

terminal42 / contao-geoip2-country example snippets




namespace App\Controller;

use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
use Contao\CoreBundle\Twig\FragmentTemplate;
use Contao\ModuleModel;
use Symfony\Component\HttpFoundation\Request;
use Terminal42\Geoip2CountryBundle\CountryProvider;

class FooController extends AbstractFrontendModuleController
{
    public function __construct(
        private readonly CountryProvider $countryProvider
    ) {
    }

    public function getResponse(FragmentTemplate $template, ModuleModel $model, Request $request): Response
    {
        // Only show content to Switzerland
        if ('CH' !== $this->countryProvider->getCurrentCountry($request)) {
            return new Response();
        }

        return $template->getResponse();
    }
}