burnbright / silverstripe-externalurlfield
为SilverStripe提供处理外部URL的DBField和FormField。
0.3.2
2016-09-12 09:40 UTC
Requires
- jakeasmith/http_build_url: *@stable
- silverstripe/framework: ^3
README
提供处理外部URL的DBField
和FormField
。
在捕获用户输入的URL时进行验证和整理。配置非常灵活。利用php的parse_url
和http_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
。