yii\i18n\Formatter::asUrl PHP Method

asUrl() public method

Formats the value as a hyperlink.
public asUrl ( mixed $value, array $options = [] ) : string
$value mixed the value to be formatted.
$options array the tag options in terms of name-value pairs. See [[Html::a()]].
return string the formatted result.
    public function asUrl($value, $options = [])
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $url = $value;
        if (strpos($url, '://') === false) {
            $url = 'http://' . $url;
        }
        return Html::a(Html::encode($value), $url, $options);
    }

Usage Example

Beispiel #1
0
 public function testAsUrl()
 {
     $value = 'http://www.yiiframework.com/';
     $this->assertSame("<a href=\"{$value}\">{$value}</a>", $this->formatter->asUrl($value));
     $value = 'https://www.yiiframework.com/';
     $this->assertSame("<a href=\"{$value}\">{$value}</a>", $this->formatter->asUrl($value));
     $value = 'www.yiiframework.com/';
     $this->assertSame("<a href=\"http://{$value}\">{$value}</a>", $this->formatter->asUrl($value));
     $value = 'https://www.yiiframework.com/?name=test&value=5"';
     $this->assertSame("<a href=\"https://www.yiiframework.com/?name=test&amp;value=5&quot;\">https://www.yiiframework.com/?name=test&amp;value=5&quot;</a>", $this->formatter->asUrl($value));
     // null display
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asUrl(null));
 }