此包的最新版本(1.0.0)没有可用的许可证信息。

单责任Uri库

1.0.0 2020-09-16 17:19 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:31 UTC


README

Latest Version Software License Total Downloads

URI类提供了帮助您从URI字符串中检索信息的函数。如果您使用URI路由,您还可以检索关于重定向段的信息。

这是一个类似于CodeIgniter URI类的库,只需实例化类即可。

安装 & 使用

composer require modularr/uri

$uri = new URI('/folder_path');

$uri = new URI($folder=null);

文件夹是可选的,但如果是子文件夹中的项目,则必须指定。

$uri->segment(n)

允许您检索特定段。其中n是要检索的段号。段从左到右编号。例如,如果您的完整URL是以下内容

段号将是以下内容

http://example.com/index.php/news/local/metro/crime_is_up

  1. news
  2. local
  3. metro
  4. crime_is_up

默认情况下,如果段不存在,函数返回FALSE(布尔值)。有一个可选的第二个参数,允许您在段缺失时设置自己的默认值。例如,这会告诉函数在失败时返回数字零

$product_id = $uri->segment(3, 0);

它有助于避免编写如下代码

if (empty($uri->segment(3)))
{
    $product_id = 0; # if segment is empty
}
else
{
    $product_id = $uri->segment(3); # get the segment
}

$uri->uri_string()

返回一个包含完整URI的字符串。例如,如果这是您的完整URL

http://example.com/index.php/news/local/345

函数将返回以下内容

/news/local/345

$uri->total_segments()

返回段的总数。