Class kartik\ipinfo\IpInfo

Inheritancekartik\ipinfo\IpInfo » kartik\base\Widget » yii\base\Widget » yii\base\Component » yii\base\BaseObject
Implementskartik\base\BootstrapInterface, yii\base\Configurable, yii\base\ViewContextInterface
Uses Traitskartik\base\BootstrapTrait, kartik\base\TranslationTrait, kartik\base\WidgetTrait
Available since version1.0

IP Info widget for Yii2 with ability to display country flag and geo position info. Uses the API from ip-api.com to parse IP info.

See also http://ip-api.com.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$api string The api to fetch IP information. kartik\ipinfo\IpInfo
$autoIdPrefix string The prefix to the automatically generated widget IDs. yii\base\Widget
$baseSourcePath string|false Get parsed base source path based on $sourcePath setting. If $sourcePath is not set, it will return the current working directory of this widget class. kartik\base\WidgetTrait
$behaviors yii\base\Behavior[] List of behaviors attached to this component. yii\base\Component
$bsColCssPrefixes array The bootstrap grid column css prefixes mapping, the key is the bootstrap versions, and the value is an array containing the sizes and their corresponding grid column css prefixes. kartik\base\BootstrapTrait
$bsCssMap array CSS conversion mappings across bootstrap library versions. kartik\base\BootstrapTrait
$bsExtBasename integer The yii2 bootstrap extension base name (readonly property available via getter method getBsExtBasename()) kartik\base\BootstrapTrait
$bsVer integer Bootstrap version number currently set (readonly property available via getter method getBsVer()) kartik\base\BootstrapTrait
$bsVersion integer|string The bootstrap library version that you wish to use for this specific extension / widget. kartik\base\BootstrapTrait
$cache boolean Whether to cache ip information in the server cache for the session. kartik\ipinfo\IpInfo
$contentHeader string The header title for content shown in the popover. kartik\ipinfo\IpInfo
$contentHeaderIcon string The icon shown before the header title for content in the popover. kartik\ipinfo\IpInfo
$counter integer A counter used to generate $id for widgets. yii\base\Widget
$defaultBtnCss string Default bootstrap button CSS (readonly property available via getter method getDefaultBtnCss()) kartik\base\BootstrapTrait
$defaultIconPrefix string Default icon prefix (readonly property available via getter method getDefaultIconPrefix()) kartik\base\BootstrapTrait
$defaultOptions array Default HTML attributes or other settings for widgets. kartik\base\WidgetTrait
$defaultPluginOptions array Default plugin options for the widget kartik\base\WidgetTrait
$detailRowOptions array The HTML attributes for each detail view row kartik\ipinfo\IpInfo
$detailViewClass string The Detail View widget class used for rendering the ip details information in a tabular form. kartik\ipinfo\IpInfo
$detailViewConfig array The configuration for the yii\widgets\DetailView widget. kartik\ipinfo\IpInfo
$dropdownClass string Bootstrap dropdown class name based on currently configured bootstrap version (readonly property available via getter method getDropdownClass()) kartik\base\BootstrapTrait
$errorData array The markup to be displayed when any exception is faced during processing by the API (e.g. no connectivity). kartik\ipinfo\IpInfo
$errorDataOptions array The HTML attributes for error data container. kartik\ipinfo\IpInfo
$errorIcon array The error icon shown for any error in the IP fetch by API. kartik\ipinfo\IpInfo
$fields array The list of names of fields/attributes to be shown in display. kartik\ipinfo\IpInfo
$flagOptions array The HTML attributes for the flag image (rendered via flag-icon-css in kartik-v/yii2-icons). kartik\ipinfo\IpInfo
$flagWrapperOptions array The HTML attributes for the flag wrapper container. kartik\ipinfo\IpInfo
$flushCache boolean Whether to flush cache for this IP before fetching. kartik\ipinfo\IpInfo
$hashVarLoadPosition integer The position where the client JS hash variables for the input widget will be loaded. kartik\base\WidgetTrait
$hideEmpty boolean Whether to hide / skip display of fields with empty values kartik\ipinfo\IpInfo
$i18n array The the internalization configuration for this widget. kartik\base\TranslationTrait
$id string|null ID of the widget. Note that the type of this property differs in getter and setter. See getId() and setId() for details. yii\base\Widget
$inlineContentOptions array The HTML attributes for container when rendering inline. kartik\ipinfo\IpInfo
$ip string The ip address. kartik\ipinfo\IpInfo
$modelClass string The model class used for rendering the ip information kartik\ipinfo\IpInfo
$moduleId string The module identifier if this widget is part of a module. kartik\base\WidgetTrait
$noData string The message to be displayed when no valid data is found. kartik\ipinfo\IpInfo
$noFlagIcon string The icon shown when no flag is found. kartik\ipinfo\IpInfo
$options array The HTML attributes for the widget container. kartik\ipinfo\IpInfo
$params array Any additional query / request parameters to send kartik\ipinfo\IpInfo
$pjaxContainerId string The identifier for the PJAX widget container if the widget is to be rendered inside a PJAX container. kartik\base\WidgetTrait
$pjaxDuplicationFix boolean Prevent duplication of pjax containers when browser back & forward buttons are pressed. kartik\base\WidgetTrait
$pluginDestroyJs string The javascript that will be used to destroy the jQuery plugin kartik\base\WidgetTrait
$pluginEvents array Widget JQuery events. kartik\base\WidgetTrait
$pluginName string The plugin name kartik\base\WidgetTrait
$pluginOptions array Widget plugin options. kartik\base\WidgetTrait
$popoverOptions array The widget configuration settings for kartik\popover\PopoverX widget that will show the details on button click / hover when $showPopover is set to true. kartik\ipinfo\IpInfo
$requestConfig array Default HTTP Client request configuration kartik\ipinfo\IpInfo
$showFlag boolean Whether to show the flag kartik\ipinfo\IpInfo
$showPopover boolean Whether to show details in a popover on click of flag. kartik\ipinfo\IpInfo
$skipFields array The list of names of fields/ attributes which will be skipped from display. kartik\ipinfo\IpInfo
$sourcePath string Directory path to the original widget source. kartik\base\WidgetTrait
$stack yii\base\Widget[] The widgets that are currently being rendered (not ended). yii\base\Widget
$template array The template configuration for rendering the popover button, popover content, or inline content. kartik\ipinfo\IpInfo
$view yii\web\View The view object that can be used to render views or view files. Note that the type of this property differs in getter and setter. See getView() and setView() for details. yii\base\Widget
$viewPath string The directory containing the view files for this widget. yii\base\Widget

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_bsVer integer Current bootstrap version number kartik\base\BootstrapTrait
$_dataVar string The HTML5 data variable name that will be used to store the Json encoded pluginOptions within the element on which the jQuery plugin will be initialized. kartik\base\WidgetTrait
$_defaultBtnCss string Default bootstrap button CSS kartik\base\BootstrapTrait
$_defaultFields array kartik\ipinfo\IpInfo
$_defaultIconPrefix string Default icon CSS prefix kartik\base\BootstrapTrait
$_defaultIcons array The default field keys and labels setting (@see initOptions method) kartik\ipinfo\IpInfo
$_encOptions string The JSON encoded plugin options. kartik\base\WidgetTrait
$_hashVar string The generated hashed variable name that will store the JSON encoded pluginOptions in yii\web\View::POS_HEAD. kartik\base\WidgetTrait
$_isBs4 boolean Flag to detect whether bootstrap 4. kartik\base\BootstrapTrait
$_msgCat string Translation message file category name for i18n. kartik\base\TranslationTrait

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addCssClass() Adds bootstrap CSS class to options by parsing the bootstrap version for the specified Bootstrap CSS type. kartik\base\BootstrapTrait
afterRun() This method is invoked right after a widget is executed. yii\base\Widget
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beforeRun() This method is invoked right before the widget is executed. yii\base\Widget
begin() Begins a widget. yii\base\Widget
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
end() Ends a widget. yii\base\Widget
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
fetchIPDetails() Returns ip information details as an array kartik\ipinfo\IpInfo
getBSClass() Gets the respective Bootstrap class based on currently configured bootstrap version. kartik\base\BootstrapTrait
getBaseSourcePath() Get parsed base source path based on $sourcePath setting. If $sourcePath is not set, it will return the current working directory of this widget class. kartik\base\WidgetTrait
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getBsVer() Gets the current set bootstrap version number. kartik\base\BootstrapTrait
getCssClass() Gets bootstrap css class by parsing the bootstrap version for the specified BS CSS type. kartik\base\BootstrapTrait
getDefaultBtnCss() Gets the default button CSS kartik\base\BootstrapTrait
getDefaultIconPrefix() Gets the default icon css prefix kartik\base\BootstrapTrait
getDetailViewAttributes() Get default attributes configuration for the detail view kartik\ipinfo\IpInfo
getDropdownClass() Gets the respective bootstrap dropdown class name based on currently configured bootstrap version. kartik\base\BootstrapTrait
getId() Returns the ID of the widget. yii\base\Widget
getView() kartik\base\WidgetTrait
getViewPath() Returns the directory containing the view files for this widget. yii\base\Widget
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\Widget
initI18N() Yii i18n messages configuration for generating translations kartik\base\TranslationTrait
isBs() Validate Bootstrap version kartik\base\BootstrapTrait
isBs4() Validate if Bootstrap 4.x version. kartik\base\BootstrapTrait
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
registerWidgetJs() Registers a JS code block for the widget. kartik\base\WidgetTrait
removeCssClass() Removes bootstrap CSS class from options by parsing the bootstrap version for the specified Bootstrap CSS type. kartik\base\BootstrapTrait
render() Renders a view. yii\base\Widget
renderFile() Renders a view file. yii\base\Widget
renderWidget() Renders the widget kartik\ipinfo\IpInfo
run() Executes the widget. kartik\ipinfo\IpInfo
setId() Sets the ID of the widget. yii\base\Widget
setView() Sets the view object to be used by this widget. yii\base\Widget
trigger() Triggers an event. yii\base\Component
widget() Creates a widget instance and runs it. yii\base\Widget

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
addAsset() Adds an asset to the view. kartik\base\WidgetTrait
configureBsVersion() Configures the bootstrap version settings kartik\base\BootstrapTrait
fixPjaxDuplication() Fix for weird PJAX container duplication behavior on pressing browser back and forward buttons. kartik\base\WidgetTrait
getAttributeConfig() Get attribute configuration for detail view kartik\ipinfo\IpInfo
getBsExtBasename() The yii2-bootstrap extension base name. kartik\base\BootstrapTrait
getPluginScript() Returns the plugin registration script. kartik\base\WidgetTrait
hashPluginOptions() Generates a hashed variable to store the pluginOptions. kartik\base\WidgetTrait
initBsVersion() Initializes bootstrap versions for the widgets and asset bundles. kartik\base\BootstrapTrait
initDestroyJs() Generates the pluginDestroyJs script if it is not set. kartik\base\WidgetTrait
initIcons() Initializes icons markup based on Bootstrap version kartik\ipinfo\IpInfo
initOptions() Initialize widget options kartik\ipinfo\IpInfo
isSameVersion() Compares two versions and checks if they are of the same major BS version. kartik\base\BootstrapTrait
isVisible() Get attribute visible setting for detail view kartik\ipinfo\IpInfo
mergeDefaultOptions() Merge default options kartik\base\WidgetTrait
parseVer() Parses and returns the major BS version kartik\base\BootstrapTrait
registerPlugin() Registers a specific plugin and the related events kartik\base\WidgetTrait
registerPluginOptions() Registers plugin options by storing within a uniquely generated javascript variable. kartik\base\WidgetTrait
setDataVar() Sets a HTML5 data variable. kartik\base\WidgetTrait

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_AFTER_RUN yii\base\WidgetEvent An event raised right after executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_BEFORE_RUN yii\base\WidgetEvent An event raised right before executing a widget. (available since version 2.0.11) yii\base\Widget
EVENT_INIT yii\base\Event An event that is triggered when the widget is initialized via init(). (available since version 2.0.11) yii\base\Widget

Property Details

$_defaultFields protected property
protected array $_defaultFields = [
    'ip''continentCode''continent''countryCode''country''flag''region''regionName''city''district''zip''lat''lon''timezone''currency''isp''org''as''asname''reverse''mobile''proxy'
]
$_defaultIcons protected property

The default field keys and labels setting (@see initOptions method)

protected array $_defaultIcons = [
    'errorIcon' => ['glyphicon glyphicon-exclamation-sign text-danger''fas fa-exclamation-circle text-danger'], 'contentHeaderIcon' => ['glyphicon glyphicon-map-marker''fas fa-map-marker-alt'], 'noFlagIcon' => ['glyphicon glyphicon-question-sign text-warning''fas fa-question-circle text-warning']
]
$api public property

The api to fetch IP information. Note the token {ip} will be replaced with the source ip address.

public string $api 'http://ip-api.com/json/{ip}'
$cache public property

Whether to cache ip information in the server cache for the session. If set to true, the Yii2 application cache component will be used for caching. If Yii2 caching component is not set or disabled, the caching will be ignored silently without any exceptions. Using caching, will optimize and reduce server api calls for ip addresses already parsed in the past.

public boolean $cache true
$contentHeader public property

The header title for content shown in the popover. Defaults to IP Position Details

public string $contentHeader null
$contentHeaderIcon public property

The icon shown before the header title for content in the popover. Defaults to:

 `<i class="glyphicon glyphicon-map-marker"></i>` for bootstrap 3.x
 `<i class="fas fa-map-marker-alt"></i>` for bootstrap 4.x
public string $contentHeaderIcon null
$detailRowOptions public property

The HTML attributes for each detail view row

$detailViewClass public property

The Detail View widget class used for rendering the ip details information in a tabular form.

public string $detailViewClass "\\yii\\widgets\\DetailView"
$detailViewConfig public property

The configuration for the yii\widgets\DetailView widget.

See also [[initOptions()]] method for the default configuration.

$errorData public property

The markup to be displayed when any exception is faced during processing by the API (e.g. no connectivity). You can set this to a blank string to not display anything. The following tokens will be replaced:

  • {errorIcon} - with the icon markup set in $errorIcon
  • {message} - any error message returned by api
public array $errorData '{errorIcon} {noData}'
$errorDataOptions public property

The HTML attributes for error data container. Defaults to: ['title' => 'IP fetch error']. The

following special tags are recognized:
- `tag`: string, the `tag` in which the content will be rendered. Defaults to `div`.
public array $errorDataOptions = [
    'class' => 'kv-ipinfo-error'
]
$errorIcon public property

The error icon shown for any error in the IP fetch by API. Defaults to:

 `<i class="glyphicon glyphicon-exclamation-sign text-danger"></i>` for bootstrap 3.x
 `<i class="fas fa-exclamation-circle text-danger"></i>` for bootstrap 4.x
public array $errorIcon null
$fields public property

The list of names of fields/attributes to be shown in display. If this is not set all attributes from $modelClass will be shown (the $hideEmpty setting will control whether to hide attributes that have an empty value)

public array $fields null
$flagOptions public property

The HTML attributes for the flag image (rendered via flag-icon-css in kartik-v/yii2-icons).

public array $flagOptions = []
$flagWrapperOptions public property

The HTML attributes for the flag wrapper container.

$flushCache public property

Whether to flush cache for this IP before fetching. This basically will clear the cached data every time if set to true. It is recommended to use and configure this property as a conditional check cleaning of the cache.

public boolean $flushCache false
$hideEmpty public property

Whether to hide / skip display of fields with empty values

public boolean $hideEmpty true
$inlineContentOptions public property

The HTML attributes for container when rendering inline.

$ip public property

The ip address. If not set will default to current user IP address.

public string $ip null
$modelClass public property

The model class used for rendering the ip information

public string $modelClass "\\kartik\\ipinfo\\IpInfoModel"
$noData public property

The message to be displayed when no valid data is found. Defaults to:

'No data found for IP address {ip}.'

This can be referred as {noData} token in any of the templates.

public string $noData null
$noFlagIcon public property

The icon shown when no flag is found. Defaults to:

 `<i class="glyphicon glyphicon-question-sign text-warning"></i>` for bootstrap 3.x
 `<i class="fas fa-question-circle text-warning"></i>` for bootstrap 4.x
public string $noFlagIcon null
$options public property

The HTML attributes for the widget container. The following special tags are recognized:

  • tag: string, the tag in which the content will be rendered. Defaults to div.
public array $options = []
$params public property

Any additional query / request parameters to send

public array $params = []
$popoverOptions public property

The widget configuration settings for kartik\popover\PopoverX widget that will show the details on button click / hover when $showPopover is set to true.

public array $popoverOptions = []
$requestConfig public property

Default HTTP Client request configuration

public array $requestConfig null
$showFlag public property

Whether to show the flag

public boolean $showFlag true
$showPopover public property

Whether to show details in a popover on click of flag. If set to false, the results will be rendered inline.

public boolean $showPopover true
$skipFields public property

The list of names of fields/ attributes which will be skipped from display. Note that this setting will override the $fields setting.

public array $skipFields = []
$template public property

The template configuration for rendering the popover button, popover content, or inline content. This

should be set as `$key => $value` pairs, where `$key` is one of:
- `popoverButton`: this is the template for popover button label (applied when `showPopover` is `true`)
- `popoverContent`: this is the template for popover content displayed on click of the button (applied when
`showPopover` is `true`)
- `inlineContent`: this is the template for inline content when `showPopover` is set to `false`
The `$value` is the template setting and can contain tags in braces, which will represent value of each IP
position field (set in [[fields]] property) fetched from the freegeoip.net API (for example `{country_code}`,
`{country_name}` etc.).  The following additional special tags will be replaced:
- '{flag}': Will be replaced with the flag icon rendered based on the `showFlag` setting.
- '{table}': Will render all fields configured via `fields` in a tabular format of labels and values.
public array $template = []

Method Details

fetchIPDetails() public method

Returns ip information details as an array

public array|mixed fetchIPDetails ( )
throws yii\base\InvalidConfigException
throws yii\httpclient\Exception
getAttributeConfig() protected method

Get attribute configuration for detail view

protected array getAttributeConfig ( $attribute )
$attribute string
getDetailViewAttributes() public method

Get default attributes configuration for the detail view

public array getDetailViewAttributes ( $model )
$model kartik\ipinfo\IpInfoModel
initIcons() protected method

Initializes icons markup based on Bootstrap version

protected void initIcons ( )
throws yii\base\InvalidConfigException
initOptions() protected method

Initialize widget options

protected void initOptions ( )
isVisible() protected method

Get attribute visible setting for detail view

protected boolean isVisible ( $attribute )
$attribute string
renderWidget() public method

Renders the widget

public void renderWidget ( )
run() public method

Executes the widget.

public string|void run ( )
return string|void

The rendering result may be directly "echoed" or returned as a string