上篇博客介绍了简易的登录功能,紧接着写一个简易的注册功能,因为注册成功后我们可以直接跳转到登录界面实现登录功能,这样就连贯起来了。
我在写注册功能的时候有用到验证码,因为没找到Phalcon框架自带的验证码所以我去多下载了一个工具:Composer。下载地址:https://www.phpcomposer.com/。具体怎么安装和配置环境详情请自行百度。
话不多说,直接上代码:
Controllers\SignController
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/5
* Time: 10:19
*/
use Phalcon\Mvc\Controller;
use Gregwar\Captcha\CaptchaBuilder;
class SignController extends Controller{
public function IndexAction(){
$builder = new CaptchaBuilder();
$builder->build();
//获取验证码图形的URL
$code_img = $builder->inline();
//将验证码的值传入session
$this->session->set('piccode',$builder->getPhrase());
//将值传入视图
$this->view->code_img = $code_img;
}
public function RegisterAction(){
$user = new User();
//获取用户传入的验证码的值
$userInput = $this->request->getPost("phrase");
//如果验证不通过
if(!$this->check($userInput)){
$this->flash->error("注册失败");
return $this->dispatcher->forward(
[
"controller"=>"sign",
"action"=>"index",
]
);
}
//验证通过存入数据
$success = $user->save($this->request->getPost(),array('name','email','password'));
//如果有数据存入数据库
if($success){
$this->flash->success("注册成功");
//跳转到登录界面
return $this->dispatcher->forward(
[
"controller"=>"log",
"action"=>'index',
]
);
}
else{
$this->flash->error("注册失败");
foreach ($user->getMessages() as $message){
echo $message->getMessage(),"<br>";
}
}
$this->view->disable();
}
private function check($userInput=''){
if($this->session->get("piccode")==$userInput){
return true;
}else{
return false;
}
}
}
views\Sign\index.phtml
<?php $this->flash->output(); ?>
<meta http-equiv="Content-Type" content=" charset=utf-8" />
<h2>欢迎注册</h2>
<?php echo $this->tag->form("../test/sign/register"); ?>
<p>
<label for="name">姓名</label>
<?php echo $this->tag->textField("name") ?>
</p>
<p>
<label for="email">邮箱</label>
<?php echo $this->tag->textField("email") ?>
</p>
<p>
<label for="password">密码</label>
<?php echo $this->tag->textField("password") ?>
</p>
<p>
验证码 <input type="text" name="phrase" />
</p>
<img src="<?php echo $code_img; ?>" />
<p>
<?php echo $this->tag->submitButton("注册") ?>
</p>
models\User.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/3/5
* Time: 9:42
*/
use\Phalcon\Mvc\Model;
use\Phalcon\Validation;
use\Phalcon\Validation\Validator\Email as EmailValidator;
use\Phalcon\Validation\Validator\PresenceOf;
use\Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
use\Phalcon\Validation\Validator\StringLength;
class User extends Model
{
public $id;
public $name;
public $email;
public $password;
public function Validation(){
$Validator = new Validation();
//用户名不为空
$Validator->add(
'name',
new PresenceOf(['message'=>'用户名不能为空',
'cancelOnFail' => true])
);
//用户名存在
$Validator->add(
'name',
new UniquenessValidator(['message'=>'用户名已存在'])
);
//用户名长度
$Validator->add(
'name',
new StringLength(array('max'=> 10,
'messageMaximum'=>'用户名长度过长'))
);
//email格式
$Validator->add(
'email',
new EmailValidator(['message'=>'email格式错误'])
);
//密码不能为空
$Validator->add(
'password',
new PresenceOf(['message'=>'密码不能为空'])
);
//密码长度
$Validator->add(
'password',
new StringLength(array(
'min'=>'6',
'messageMinimum'=>'密码长度不小于6个字符',
'max'=>'15',
'messageMaximum'=>'密码长度不大于15个字符'))
);
return $this->Validate($Validator);
}
}
运行结果:
注册页面:
注册成功跳到登录界面(登录在上一篇有写过):
登录成功:
注册的数据成功写入数据库:
User表:
这样我们就成功注册了。
笔者水平有限,还会继续努力,请多多指教。