fromholdio/silverstripe-externalurlfield

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

安装次数: 19,820

依赖项: 4

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 9

类型:silverstripe-vendormodule

1.1.0 2023-05-18 06:08 UTC

This package is auto-updated.

Last update: 2024-09-18 08:55:49 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

提供处理外部 URL 的 DBFieldFormField

验证并整理用户捕获的 URL。配置非常灵活。使用 php 的 parse_urlhttp_build_url 进行实际工作。

安装

注意 - 此项目是从 burnbright/silverstripe-externalurlfield 分支出来的,并更新到新的 composer 供应商命名空间;使客户端项目的 composer 安装更容易。

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

要求

使用来自 PECL pecl_http 库http_build_url 函数。然而,模块的 composer 要求包括一个 PHP 后备/模拟/填充。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