今天终于发布了自己在composer的第一个包

虽然是迟了一些,但是总算是迈出了为人类软件事业贡献的第一步。

陆续会有更多的自己十年代码累积的包推出,敬请期待。每个包都是相当轻量级的,贯彻这十年来的软件开发宗旨:CoolLittleThings,Keep it Simple, Stuipid!

第一个包介绍:URL处理包,相当实用,但外面所见不多(都好像太重了)

https://packagist.org/packages/rayful/url

URL

一个对URL的通用操作类。

安装(Composer)

composer require rayful/url

基本用法:

$URL = new URL("http://abc.com/?page=1&param=a");
$URL->setQuery(["page"=>2]);
$newUrl = $URL->build();

echo $newUrl;   //will output: http://abc.com/?page=2&param=a

$URL = new URL("http://abc.com/?page=1&param=a");
$URL->replaceQuery(["page"=>2]);
$newUrl = $URL->build();

echo $newUrl;   //will output: http://abc.com/?page=2

可以全局地urlencode整条URL:

$URL = new URL("http://abc.com/目录1/目录2/index.php?cat=科技&rate= 非常好");
$URL->encode();
$newUrl = $URL->build();

echo $newUrl;   //http://abc.com/%E7_%AE%E5%BD_1/%E7_%AE%E5%BD_2/index.php?cat=%E7%A7_%E6__&rate=+%E9__%E5%B8%B8%E5%A5%BD

构造函数不传参,默认为改变当前URL(需要在浏览器内)的某个参数:

$URL = new URL();
$URL->setQuery(["page"=>2]);
$newUrl =  $URL->build();

快捷方式:

echo URL::append(['page'=>2]);

四个静态方法的调用:

$domain = URL::domain($url);    //url.com
$queryString = URL::queryString($url);  //query=1&query=2
$query = URL::query($url);  //['query'=>1,'query2'=>2]
$dir = URL::dir($url);  //http://url.com/doc

Leave a Reply

Your email address will not be published. Required fields are marked *