burnbright/silverstripe-externalurlfield

为SilverStripe提供处理外部URL的DBField和FormField。

安装数: 9,416

依赖者: 1

建议者: 0

安全性: 0

星标: 10

关注者: 3

分支: 9

开放问题: 10

类型:silverstripe-vendormodule

0.3.2 2016-09-12 09:40 UTC

This package is auto-updated.

Last update: 2024-08-28 17:08:22 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

提供处理外部URL的DBFieldFormField

在捕获用户输入的URL时进行验证和整理。配置非常灵活。利用php的parse_urlhttp_build_url函数进行实际工作。

安装

composer require burnbright/silverstripe-externalurlfield "*@stable"

需求

使用PECL pecl_http库中的http_build_url函数。然而,模块的composer需求包括一个PHP回退/shim/polyfill。composer替换会检查http_build_url的存在。

  • SilverStripe ^4

数据对象/模板使用

ExternalURL类(Varchar)处理。

use SilverStripe\ORM\DataObject;

class MyDataObject extends DataObject
{
    private static $db = array(
        'Website' => 'ExternalURL'
    );
}
<% with $MyDataObject %>
    <p>Website: $Website</p>
    <p>Website Nice: $Website.Nice</p>
    <p>Website Domain: $Website.Domain</p>
    <p>Website Domain No WWW: $Website.Domain.NoWWW</p>
<% end_with %>

给定URL http://username:password@www.hostname.com:81/path?arg=value#anchor,上述操作将产生

Website: http://username:password@www.hostname.com:81/path?arg=value#anchor
Website Nice: www.hostname.com/path
Website Domain: www.hostname.com
Website Domain No WWW: hostname.com

表单使用

ExternalURLField(FormField)处理。

验证由html5 pattern属性处理,同时也由更健壮的正则表达式在服务器端处理。字段使用html5 type="url"属性。

您可以配置URL的各个部分,以在缺失时去除或填充默认值。

use BurnBright\ExternalURLField\ExternalURLField;

//default
$websitefield = new ExternalURLField('Website');

//set options (with defaults shown)
$websitefield->setConfig(array(
    //these will be added, if missing
    'defaultparts' => array(
        'scheme' => 'http'
    ),
    //these parts are removed from saved urls
    'removeparts' => array(
        'scheme' => false,
        'user' => true,
        'pass' => true,
        'host' => false,
        'port' => false,
        'path' => false,
        'query' => false,
        'fragment' => false
    ),
    'html5validation' => true
));

//say you want to store nice tidy facebook urls
$websitefield->setConfig('removeparts',array(
    'query' => true,
    'fragment' => 'true',
    'port' => 'true'
));
//a urls like https://#/joe.bloggs?fref=nf&pnref=story
//would become https://#/joe.bloggs

HTML5验证

默认启用,html5验证将字段类型属性设置为url,并添加一个设置为https?://.+的模式属性。

禁用使用html5validation配置

$field->setConfig("html5validation", false);

禁用html5验证特别有用,如果您想允许用户输入没有方案/协议的URL,例如:mywebsite.com而不是http://www.mywebsite.com