邢栋博客
邢栋博客,Action博客,记录工作和生活中的点点滴滴
yii2设置默认时间时区(基础模板和高级模板)
基本模板
config/web.php
在$config中加入 'timeZone'=>'Asia/Shanghai',
高级模板
common/config/main.php
在return中加入 'timeZone'=>'Asia/Shanghai',
config/web.php
在$config中加入 'timeZone'=>'Asia/Shanghai',
高级模板
common/config/main.php
在return中加入 'timeZone'=>'Asia/Shanghai',
yii2创建自己的扩展
1.建立yii2-helloxd仓库
flyaction/yii2-helloxd
2.添加仓库内容,并初始化composer,提交到packagist
2.1 添加仓库内容,并初始化composer,
mkdir composer-yii2 //创建目录
cd composer-yii2
git clone https://github.com/flyaction/yii2-helloxd.git //克隆
cd yii2-helloxd
composer init
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [root/hello-yii2]: flyaction/yii2-helloxd
Description []: say hello for yii
Author [, n to skip]: n
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: yii2-extension
License []: BSD-3-Clause
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
{
"name": "flyaction/yii2-helloxd",
"description": "say hello for yii",
"type": "yii2-extension",
"license": "BSD 3-Clause",
"minimum-stability": "dev",
"require": {}
}
Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]? yes
2.2修改生成的composer.json
{
"name": "flyaction/yii2-helloxd",
"description": "say hello for yii2",
"type": "yii2-extension",
"license": "BSD-3-Clause",
"require": {
"yiisoft/yii2": "*"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"flyaction\\helloxd\\": ""
}
}
}
2.3在yii2-helloxd 下新建Helloxd.php,内容如下
<?php
namespace flyaction\helloxd;
class Helloxd
{
public static function sayHello()
{
return "hello";
}
}
2.4提交至仓库,并打上标签
git commit -a -m 'first commit'
git push
git tag -a v1.0.0 -m '1.0.0版本'
git push --tags
3.发布 到 packagist
进入https://packagist.org/,登陆后点击右上角的submit按钮,之后输入你的 github 仓库地址,点击 check 之后 submit
仓库地址: https://github.com/flyaction/yii2-helloxd.git
4.测试使用
进入我们的yii项目
composer require flyaction/yii2-helloxd
安装完毕后,进入一个控制器的方法,写入一些内容即可
echo \flyaction\helloxd\Helloxd::sayHello();
flyaction/yii2-helloxd
2.添加仓库内容,并初始化composer,提交到packagist
2.1 添加仓库内容,并初始化composer,
mkdir composer-yii2 //创建目录
cd composer-yii2
git clone https://github.com/flyaction/yii2-helloxd.git //克隆
cd yii2-helloxd
composer init
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [root/hello-yii2]: flyaction/yii2-helloxd
Description []: say hello for yii
Author [, n to skip]: n
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: yii2-extension
License []: BSD-3-Clause
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
{
"name": "flyaction/yii2-helloxd",
"description": "say hello for yii",
"type": "yii2-extension",
"license": "BSD 3-Clause",
"minimum-stability": "dev",
"require": {}
}
Do you confirm generation [yes]? yes
Would you like the vendor directory added to your .gitignore [yes]? yes
2.2修改生成的composer.json
{
"name": "flyaction/yii2-helloxd",
"description": "say hello for yii2",
"type": "yii2-extension",
"license": "BSD-3-Clause",
"require": {
"yiisoft/yii2": "*"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"flyaction\\helloxd\\": ""
}
}
}
2.3在yii2-helloxd 下新建Helloxd.php,内容如下
<?php
namespace flyaction\helloxd;
class Helloxd
{
public static function sayHello()
{
return "hello";
}
}
2.4提交至仓库,并打上标签
git commit -a -m 'first commit'
git push
git tag -a v1.0.0 -m '1.0.0版本'
git push --tags
3.发布 到 packagist
进入https://packagist.org/,登陆后点击右上角的submit按钮,之后输入你的 github 仓库地址,点击 check 之后 submit
仓库地址: https://github.com/flyaction/yii2-helloxd.git
4.测试使用
进入我们的yii项目
composer require flyaction/yii2-helloxd
安装完毕后,进入一个控制器的方法,写入一些内容即可
echo \flyaction\helloxd\Helloxd::sayHello();
yii2控制器中跳转带提示语
控制器中
<?php
use yii\helpers\Url;
public function actionDelete($id)
{
$model = new LiveUser;
$params = array();
$params['id'] = $id;
$result = $model->findOne($params)->delete();//删除
if($result){
$message = "删除成功";
$error = false;
}else{
$message = "删除失败";
$error = true;
}
$delay = 3;
return $this->renderPartial('/system/redirect', array(
'message'=> $message,
'url'=> Url::to(['user/index']),
'error'=>$error,
'delay'=> $delay,
));
}
?>
/system/redirect 视图
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>页面跳转提示</title>
<style type="text/css">
body{ margin:0px; padding:0px; font-size:12px;background:#FFFFFF; font-family: '微软雅黑'; font-size:13px; color:#666666;}
a{ font-size:13px; color:#2571be; text-decoration:none;}
#cont{ width:60%; height:200px; border:1px solid #CCCCCC; margin:0px auto; margin-top:100px; background:#FFFFFF;}
#cont_main{ width:80%; height:120px; margin:0px auto; margin-top:50px;}
#cont_main_l{ width:20%; height:100px; float:left; text-align:center; }
#cont_main_r{ width:80%; height:120px; float:left; line-height:30px;}
#cont_main_r .a{ font-size:16px; font-weight:bold;}
</style>
</head>
<body>
<div style="background:#FFFFFF; height:500px;">
<div id="cont">
<div id="cont_main">
<div id="cont_main_l">
<?php if($error) {?>
<img src="statics/img/icon_failed.png" />
<?php }else{?>
<img src="statics/img/icon_success.png" />
<?php }?>
</div>
<div id="cont_main_r">
<span class="a"><?php echo($message); ?></span><br />
<span>提示你可以进行以下操作:</span><br />
系统将在 <b id="wait"><?php echo($delay); ?></b> 秒钟会自动跳转 <a id="href" href="<?php echo($url); ?>">点击跳转</a></span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function(){
var wait = document.getElementById('wait'),href = document.getElementById('href').href;
var interval = setInterval(function(){
var time = --wait.innerHTML;
if(time <= 0) {
location.href = href;
clearInterval(interval);
};
}, 1000);
})();
</script>
</body>
</html>
<?php
use yii\helpers\Url;
public function actionDelete($id)
{
$model = new LiveUser;
$params = array();
$params['id'] = $id;
$result = $model->findOne($params)->delete();//删除
if($result){
$message = "删除成功";
$error = false;
}else{
$message = "删除失败";
$error = true;
}
$delay = 3;
return $this->renderPartial('/system/redirect', array(
'message'=> $message,
'url'=> Url::to(['user/index']),
'error'=>$error,
'delay'=> $delay,
));
}
?>
/system/redirect 视图
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>页面跳转提示</title>
<style type="text/css">
body{ margin:0px; padding:0px; font-size:12px;background:#FFFFFF; font-family: '微软雅黑'; font-size:13px; color:#666666;}
a{ font-size:13px; color:#2571be; text-decoration:none;}
#cont{ width:60%; height:200px; border:1px solid #CCCCCC; margin:0px auto; margin-top:100px; background:#FFFFFF;}
#cont_main{ width:80%; height:120px; margin:0px auto; margin-top:50px;}
#cont_main_l{ width:20%; height:100px; float:left; text-align:center; }
#cont_main_r{ width:80%; height:120px; float:left; line-height:30px;}
#cont_main_r .a{ font-size:16px; font-weight:bold;}
</style>
</head>
<body>
<div style="background:#FFFFFF; height:500px;">
<div id="cont">
<div id="cont_main">
<div id="cont_main_l">
<?php if($error) {?>
<img src="statics/img/icon_failed.png" />
<?php }else{?>
<img src="statics/img/icon_success.png" />
<?php }?>
</div>
<div id="cont_main_r">
<span class="a"><?php echo($message); ?></span><br />
<span>提示你可以进行以下操作:</span><br />
系统将在 <b id="wait"><?php echo($delay); ?></b> 秒钟会自动跳转 <a id="href" href="<?php echo($url); ?>">点击跳转</a></span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function(){
var wait = document.getElementById('wait'),href = document.getElementById('href').href;
var interval = setInterval(function(){
var time = --wait.innerHTML;
if(time <= 0) {
location.href = href;
clearInterval(interval);
};
}, 1000);
})();
</script>
</body>
</html>
Connection could not be established with host smtp.exmail.qq.com [ #0]
今天在用yii2发送邮件的时候提示
Connection could not be established with host smtp.exmail.qq.com [ #0]
openssl没啥问题,函数权限也有打开了
折腾半天后终于找到解决办法
出问题之前的代码
/common/config/main-local.php中mailer配置
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,//false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
'transport'=>[
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com',
'username' => '这里是公司的邮箱账号',
'password' => '邮箱密码',
'port' => '465',
'encryption' => 'ssl',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['这里是公司的邮箱账号'=>'Admin']
],
],
控制器中代码
$mail = Yii::$app->mailer->compose();
$mail->setTo('xingdong1117@126.com');
$mail->setSubject('测试邮件');
$mail->setHtmlBody('这里是内容'); //发布可以带html标签的文本
if ($mail->send()) {
echo "success";
}else {
echo "failse";
}
解决办法
在/common/config/main-local.php中mailer配置
'transport'=>[
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com',
'username' => '这里是公司的邮箱账号',
'password' => '邮箱密码',
'port' => '465',
'encryption' => 'ssl',
//这里是新加入的内容
'StreamOptions'=>[
'ssl'=>[
'verify_peer' => false, //是否需要验证 SSL 证书
'verify_peer_name'=>false,//Require verification of peer name.
'allow_self_signed' => true//是否允许自签名证书
],
]
],
//更多参数可以查看 http://php.net/manual/zh/context.ssl.php
其实在
在vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php 266行代码中
$streamContext = stream_context_create($options);
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
中间加入
$streamContext = stream_context_create($options);
//----
stream_context_set_option($streamContext, 'ssl', 'verify_peer',false);
stream_context_set_option($streamContext, 'ssl', 'verify_peer_name',false);
stream_context_set_option($streamContext, 'ssl', 'allow_self_signed',true);
//-----
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
也得到了解决,不过不如上面的办法
如果在使用phpmailer的时候也出现此问题,可以这样解决
$mail = new PHPMailer;
//$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.exmail.qq.com';
$mail->SMTPAuth = true;
$mail->Username = '这里是公司的邮箱账号';
$mail->Password = '邮箱密码';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name'=>false,
'allow_self_signed' => true
)
);
$mail->setFrom('这里是公司的邮箱账号','Admin');
$mail->addAddress('xingdong1117@126.com');
$mail->addReplyTo('这里是公司的邮箱账号', 'Admin');
$mail->isHTML(true);
$mail->Subject = '测试邮件';
$mail->Body = '邮件内容';
if ($mail->send()) {
echo "success";
}else {
echo "failse";
}
ps:不知道啥原因导致的这样的情况,我本地是mac环境,没有这样的问题,到了linux服务器上就出现了这样的问题,只有加上这几个参数才可以。等找到真正原因再来写。
Connection could not be established with host smtp.exmail.qq.com [ #0]
openssl没啥问题,函数权限也有打开了
折腾半天后终于找到解决办法
出问题之前的代码
/common/config/main-local.php中mailer配置
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,//false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
'transport'=>[
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com',
'username' => '这里是公司的邮箱账号',
'password' => '邮箱密码',
'port' => '465',
'encryption' => 'ssl',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['这里是公司的邮箱账号'=>'Admin']
],
],
控制器中代码
$mail = Yii::$app->mailer->compose();
$mail->setTo('xingdong1117@126.com');
$mail->setSubject('测试邮件');
$mail->setHtmlBody('这里是内容'); //发布可以带html标签的文本
if ($mail->send()) {
echo "success";
}else {
echo "failse";
}
解决办法
在/common/config/main-local.php中mailer配置
'transport'=>[
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com',
'username' => '这里是公司的邮箱账号',
'password' => '邮箱密码',
'port' => '465',
'encryption' => 'ssl',
//这里是新加入的内容
'StreamOptions'=>[
'ssl'=>[
'verify_peer' => false, //是否需要验证 SSL 证书
'verify_peer_name'=>false,//Require verification of peer name.
'allow_self_signed' => true//是否允许自签名证书
],
]
],
//更多参数可以查看 http://php.net/manual/zh/context.ssl.php
其实在
在vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php 266行代码中
$streamContext = stream_context_create($options);
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
中间加入
$streamContext = stream_context_create($options);
//----
stream_context_set_option($streamContext, 'ssl', 'verify_peer',false);
stream_context_set_option($streamContext, 'ssl', 'verify_peer_name',false);
stream_context_set_option($streamContext, 'ssl', 'allow_self_signed',true);
//-----
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
也得到了解决,不过不如上面的办法
如果在使用phpmailer的时候也出现此问题,可以这样解决
$mail = new PHPMailer;
//$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.exmail.qq.com';
$mail->SMTPAuth = true;
$mail->Username = '这里是公司的邮箱账号';
$mail->Password = '邮箱密码';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name'=>false,
'allow_self_signed' => true
)
);
$mail->setFrom('这里是公司的邮箱账号','Admin');
$mail->addAddress('xingdong1117@126.com');
$mail->addReplyTo('这里是公司的邮箱账号', 'Admin');
$mail->isHTML(true);
$mail->Subject = '测试邮件';
$mail->Body = '邮件内容';
if ($mail->send()) {
echo "success";
}else {
echo "failse";
}
ps:不知道啥原因导致的这样的情况,我本地是mac环境,没有这样的问题,到了linux服务器上就出现了这样的问题,只有加上这几个参数才可以。等找到真正原因再来写。