ZF2 Http uri验证

dir*_*ory 4 validation uri zend-framework2

我尝试用ZF2验证URI.问题是当我将$ value设置为'http://google.com'或'google/com'时,它会给我输出'bool(false)'.

我的代码;

use Zend\Uri\Http as ValidateUri;
use Zend\Uri\Exception as UriException;


    class Domain
    {
        public function domain($value)
        {
            return $this->validate($value);
        }

        public function validate($value)
        {   
            if (empty($value) || !is_string($value)) {
                return false;
            }

            try {
                $uriHttp = ValidateUri::validateHost($value);
                var_dump($uriHttp);
            } catch (UriException $e) {
                return false;
            }

            return true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢!缺口

kie*_*iak 5

我建议使用验证器Zend\Validator\Hostname.文档示例:

$hostname  = 'http://google.com';
$validator = new Zend\Validator\Hostname(Zend\Validator\Hostname::ALLOW_DNS);

if ($validator->isValid($hostname)) {
    // hostname appears to be valid
   echo 'Hostname appears to be valid';
} else {
    // hostname is invalid; print the reasons
    foreach ($validator->getMessages() as $message) {
        echo "$message\n";
    }
}
Run Code Online (Sandbox Code Playgroud)