sunnysideup/silverstripe-externalurlfield

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

安装: 402

依赖项: 3

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 9

类型:silverstripe-vendormodule

3.1.0 2024-03-13 03:17 UTC

This package is auto-updated.

Last update: 2024-09-15 22:57:35 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

提供处理外部 URL 的 DBFieldFormField

验证并整理从用户处捕获的 URL。配置高度灵活。使用 php 的 parse_urlhttp_build_url 来执行实际工作。

安装

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

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

需求

使用来自 PECL pecl_http 库的 http_build_url 函数。但是,模块的 composer 需求包括一个 PHP 回退/模拟/补丁。composer 替换会检查 http_build_url 的存在。

  • SilverStripe ^4 || ^5

DataObject / 模板使用

ExternalURL 类(Varchar)处理。

use SilverStripe\ORM\DataObject;

class MyDataObject extends DataObject
{
    private static $db = array(
        'Website' => 'ExternalURL(768)', // set a max length so that we can index it - if you do not need to index it then you may not need to add this.
    );
}
<% 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 Sunnysideup\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