yii2tech/https
最新稳定版本:1.0.0
Composer 安装命令:
composer require yii2tech/https
包简介
Tools for the secure connection (https) handling
README 文档
README
Secure connection (https) handling extension for Yii2
This extension provides some tools for the secure connection (https) handling.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/https
or add
"yii2tech/https": "*"
to the require section of your composer.json.
Usage
This extension provides some tools for the secure connection (https) handling.
Filter [[\yii2tech\https\SecureConnectionFilter]] allows automatic redirection from 'http' to 'https' protocol,
depending of which one is required by particular action. Actions separation into those requiring secure protocol
and the ones requiring unsecure protocol can be setup via secureOnly and secureExcept properties.
Being descendant of [[yii\base\ActionFilter]], [[\yii2tech\https\SecureConnectionFilter]] can be setup both at the controller level and at module (application) level.
Application configuration example:
return [ 'as https' => [ 'class' => 'yii2tech\https\SecureConnectionFilter', 'secureOnly' => [ 'site/login', 'site/signup', ], ], // ... ];
Controller configuration example:
use yii\web\Controller; use yii2tech\https\SecureConnectionFilter; class SiteController extends Controller { public function behaviors() { return [ 'https' => [ 'class' => SecureConnectionFilter::className(), 'secureOnly' => [ 'login', 'signup', ], ], ]; } // ... }
Heads up! Do not forget about only and except properties of the filter. Keep in mind that secureOnly
and secureExcept can not affect those actions, which are excluded from filtering via only and except.
You may use this to skip some actions from the secure connection processing.
Heads up! Be aware of the forms, which may appear at on protocol but require submission to the other. Request body can not be transferred during redirect, so submitted data will be lost. You'll have to setup form action manually with the correct schema, instead of relying on the filter.
Automatic URL creation
Using simple redirect from one protocol to another is not efficient and have a risk of loosing data submitted via web form. Thus it is better to explicitly specify URL with correct protocol in your views. You may simplify this process using [[\yii2tech\https\SecureUrlRuleFilter]] action filter. Once applied it will adjust [[\yii\web\UrlManager::rules]] in the way [[\yii\web\UrlManager::createUrl()]] method will automatically create absolute URL with correct protocol in case it miss matches current one.
Application configuration example:
return [ 'as secureUrlRules' => [ 'class' => 'yii2tech\https\SecureUrlRuleFilter', 'secureOnlyRoutes' => [ 'auth/login', 'site/signup', ], 'secureExceptRoutes' => [ 'site/index', 'help/<action>', ], ], 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '/' => 'site/index', 'login' => 'auth/login', 'signup' => 'site/signup', '<action:contact|faq>' => 'help/<action>', ] ], ], // ... ];
Now [[\yii\web\UrlManager::createUrl()]] will create URLs with correct protocol without extra efforts:
if (Yii::$app->request->isSecureConnection) { echo Yii::$app->urlManager->createUrl(['site/index']); // outputs: 'http://domain.com/' echo Yii::$app->urlManager->createUrl(['auth/login']); // outputs: '/login' } else { echo Yii::$app->urlManager->createUrl(['site/index']); // outputs: '/' echo Yii::$app->urlManager->createUrl(['auth/login']); // outputs: 'https://domain.com/login' }
Note: [[\yii2tech\https\SecureUrlRuleFilter]] filter will take affect only if [[\yii\web\UrlManager::enablePrettyUrl]] is enabled.
Heads up! once applied [[\yii2tech\https\SecureUrlRuleFilter]] filter changes the state of related [[\yii\web\UrlManager]] instance, which may make unexpected side effects. For example: this may break such features as parsing URL.
The more reliable way for automatic URL creation is usage of [[\yii2tech\https\SecureConnectionUrlManagerTrait]].
Being used with the descendant of the [[\yii\web\UrlManager]] it will adjust createUrl() method so it will behave
exactly the same as in example above.
Trait usage example:
namespace app\components\web; use yii2tech\https\SecureConnectionUrlManagerTrait; class MyUrlManager extends \yii\web\UrlManager { use SecureConnectionUrlManagerTrait; }
Application configuration example:
return [ 'components' => [ 'urlManager' => [ 'class' => 'app\components\web\MyUrlManager', 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '/' => 'site/index', 'login' => 'auth/login', 'signup' => 'site/signup', '<action:contact|faq>' => 'help/<action>', ], 'secureOnlyRoutes' => [ 'site/signup', 'auth/*', ], 'secureExceptRoutes' => [ 'site/index', 'help/*', ], ], ], // ... ];
In case you do not use any custom URL manager in your project you can use [[\yii2tech\https\UrlManager]], which already have [[\yii2tech\https\SecureConnectionUrlManagerTrait]] applied.
Note: usage of [[\yii2tech\https\SecureConnectionUrlManagerTrait]] is more reliable then [[\yii2tech\https\SecureUrlRuleFilter]], but it may consume more computing resources at some cases. Still it is recommended to use trait instead of filter.
Note: [[\yii2tech\https\SecureConnectionUrlManagerTrait]] and [[\yii2tech\https\SecureUrlRuleFilter]] process routes in the different way: filter uses those defined by URL rules, while trait operates exact route names as they passed to
createUrl()method.
yii2tech/https 适用场景与选型建议
yii2tech/https 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.07k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2016 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「https」 「secure」 「yii2」 「secure connection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2tech/https 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2tech/https 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2tech/https 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure Trading driver for the Omnipay PHP payment processing library
A custom URL rule class for Yii 2 which allows to create translated URL rules
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A Laravel artisan based package to create the AWS (SES + SNS) infrastructure to receive email event notifications with Http/Https endpoint.
URL routing framework and requests/responses handler for PHP
Library implement an Online Certificate Status Protocol (OCSP) responder
统计信息
- 总下载量: 2.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-05-18