szajens/pece

Pece路由器为Codeigniter 4 - 此库修改URL助手和URI服务的宿主

1.0 2022-05-22 20:22 UTC

This package is auto-updated.

Last update: 2024-09-11 22:53:32 UTC


README

此库修改URL助手和URI服务的宿主

如何在路由中使用

CI4路由中有新选项,你可以使用全部或部分

示例

$routes->get('/test/(:any)', 'Test::index/$1', ['pSubdomain'=> '[a-z]+', 'pDomain'=>'ci.loc', 'pSSL' => null, 'pSubhost' => false]);

限制到子域名

当存在子域名选项时,系统将限制路由仅在相应的子域名中可用。如果通过应用程序查看的子域名是正确的,则将匹配该路由

正则表达式 如果你更喜欢,可以使用正则表达式来定义你的子域名规则。允许任何有效的正则表达式

示例

// Limit to media.example.com
$routes->add('from', 'to', ['pSubdomain' => 'media']); or
$routes->add('from', 'to', ['pSubdomain' => '[a-z]+']);

限制到域名

你可以通过传递“pDomain”选项和所需的域名,将路由组限制为仅在应用程序的特定域名中运行:示例

$routes->get('from', 'to', ['pDomain' => 'example.com']);

限制到SSL

如果你需要限制到https,使用true;要限制到http,设置false或不限制设置null。示例

$routes->get('from', 'to', ['pSSL' => true]);

将子域名+域名作为主机

如果你想在CI4中使用子域名和域名作为主机,将此选项设置为true或false,否则示例

$routes->get('from', 'to', ['pSubhost' => true]);

如何在你的应用程序中使用,例如控制器

从子域名更改主机:Services::pece()->setDomainInCI();

xxx.host.com => host.com

从域名更改到子域名(如果请求来自子域名,则反转setDomainInCI()):Services::pece()->setSubdomainInCI();

host.com => xxx.host.com

更改主机中的子域名:Services::pece()->changeSubdomainInCI('new.sub.domain');

host.com => new.sub.domain.host.com
abc.host.com => new.sub.domain.host.com

向主机中添加子域名:Services::pece()->addSubdomainInCI('new.sub.domain');

host.com => new.sub.domain.host.com
abc.host.com => new.sub.abc.domain.host.com

其他方法

Services::pece()->
getDomain(): - return request domain
getSubdomain(): - request subdomain
getScheme(): - request scheme e.g. 'https://' or 'http://'

安装

  • Composer
composer require szajens/pece