degordian/yii2-geofencing

Yii2 ActionFilter for allowing or denying access based on the visitors geo location

Installs: 84

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 9

Forks: 2

Type:yii2-extension

v1.0.0 2017-06-13 10:48 UTC

This package is not auto-updated.

Last update: 2024-12-22 04:32:32 UTC


README

An action filter that lets you allow or deny actions based on the visitors location.

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist degordian/yii2-geofencing "~1.0"

or add

"degordian/yii2-geofencing": "~1.0"

to the require section of your composer.json file.

Usage

Once the extension is installed, add it to your controller

  public function behaviors() {
    return [
      'class' => GeoIpAccessControl::class,
      'isoCodes' => ['HR', 'SI', 'RS'], //ISO 3166-1 alpha-2 two letter country code
      'filterMode' => GeoIpFilterMode::ALLOW, //allows only if you are listed in isoCodes,
      #'filterMode' => GeoIpFilterMode::DENY //allows only if you are not listed in isoCodes
      'getIp' => function() {
        //you can provide a custom function used to get the clients IP
        //defaults to Yii::$app->request->getUserIP()
      },
      'getIsoCode' => function($ip) {
        //you can provide a custom function used to get the iso code from the clients IP
        //by default uses lysenkobv/yii2-geoip
      },
      'message' => 'The message to display if the content is denied'
    ]
  }

Since this extends ActionFilter, you can use 'only' and 'except' to fine tune the geofencing criteria