PHP code example of yii2tech / https
1. Go to this page and download the library: Download yii2tech/https library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2tech / https example snippets
return [
'as https' => [
'class' => 'yii2tech\https\SecureConnectionFilter',
'secureOnly' => [
'site/login',
'site/signup',
],
],
// ...
];
use yii\web\Controller;
use yii2tech\https\SecureConnectionFilter;
class SiteController extends Controller
{
public function behaviors()
{
return [
'https' => [
'class' => SecureConnectionFilter::className(),
'secureOnly' => [
'login',
'signup',
],
],
];
}
// ...
}
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>',
]
],
],
// ...
];
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'
}
namespace app\components\web;
use yii2tech\https\SecureConnectionUrlManagerTrait;
class MyUrlManager extends \yii\web\UrlManager
{
use SecureConnectionUrlManagerTrait;
}
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/*',
],
],
],
// ...
];
php composer.phar