werx / url
构建应用程序中资源的URL
Requires
- php: >= 5.4
- league/plates: 2.1.0
- rize/uri-template: 0.2.*
Requires (Dev)
- phpunit/phpunit: 4.4.*
- squizlabs/php_codesniffer: 1.*
This package is auto-updated.
Last update: 2024-08-29 03:18:29 UTC
README
构建应用程序中资源的URL。可作为独立库或Plates 扩展使用。
此库使用rize\UriTemplate进行重负载的URL扩展,并增加了构建应用程序中资源URL的额外功能,使其更加容易。
使用方法
创建URL构建器实例。
获取URL构建器实例。如果您不传递构造函数值,它将基于当前执行的脚本的根相对路径(使用$_SERVER['SCRIPT_NAME']
)来构建所有URL。
$builder = new \werx\Url\Builder;
您也可以提供基本URL和脚本名称。
$builder = new \werx\Url\Builder('/path/to/app/', 'index.php');
如果您在构造函数中设置了一个完全限定的URL,则在构建URL时将使用它。
$builder = new \werx\Url\Builder('http://example.com/path/to/app/', 'index.php');
构建URL。
使用单个id
进行简单模式替换
$url = $builder->action('home/details/{id}', 5); var_dump($url); # /path/to/app/index.php/home/details/5
多个模式替换
$url = $builder->action('home/{action}/{id}', ['action' => 'details', 'id' => 5]); var_dump($url); # /path/to/app/index.php/home/details/5
将数组转换为查询字符串。
$url = $builder->query('home/search', ['name' => 'Josh', 'state' => 'AR']); var_dump($url); # /path/to/app/index.php/home/search?name=Josh&state=AR
构建指向应用程序中静态资源的URL。
$url = $builder->asset('images/logo.png'); var_dump($url); # /path/to/app/images/logo.png
Plates 扩展
此库包含一个扩展,允许其在Plates 模板中使用。
在您的控制器...
$engine = new \League\Plates\Engine('/path/to/templates'); $ext = new \werx\Url\Extensions\Plates; $engine->loadExtension($ext); $template = new \League\Plates\Template($engine); $output = $template->render('home');
然后在您的视图中,您可以通过$this->url()->methodtocall()
调用任何werx\Url\Builder
方法。
<a href="<?=$this->url()->action(home/details/{id}, 5)?>">Details</a> <img src="<?=$this->url()->asset('images/logo.png')?>"/>
安装
此软件包可以通过Composer以werx/url
安装和自动加载。如果您不熟悉PHP的Composer依赖管理器,您应该先阅读此内容。
$ composer require werx/url --prefer-dist
贡献
单元测试
$ vendor/bin/phpunit
编码标准
此库使用PHP_CodeSniffer来确保遵循编码标准。
我采用了PHP FIG PSR-2 编码标准,除了缩进(制表符 vs. 空格)规则之外。PSR-2说使用4个空格。我使用制表符。无需讨论。
为了支持使用制表符缩进,我定义了一个自定义PSR-2规则集,该规则集扩展了标准PHP_CodeSniffer使用的PSR-2规则集。您可以在本项目的根目录中找到此规则集,名为PSR2Tabs.xml。
从本项目的根目录执行codesniffer命令以使用这些自定义规则运行sniffer。
$ ./codesniffer