以下过程中的变量 数据库字段均按 cakephp要求进行配置
1.读取当前用户名字username:
$username = $this->Auth->user('username');
2.更新某个字段:
不知道为啥,按照官方文档用save前指明表中主键的方法更新字段都默认插入一个新的,还好找到了其他解决方案
$time = time(); $username = $this->Auth->user('username'); $last="'".date("Y-m-d h:i:s",$time)."'"; $this->User->updateAll( array('User.modified' => $last), array('User.username' => $username) );
3.表单操作:
<?php echo $this->Form->create('User', array('action' => 'login')); ?> <?php echo $this->Form->input('username'); echo $this->Form->input('password');?> <?php echo $this->Form->end(__('Login')); ?>对应html表单:
<form id="UserLoginForm" method="post" action="/users/login"> <input name="data[User][username]" type="text" placeholder="ID" required="required" > <input name="data[User][password]" placeholder="密码" type="password"required="required" > <button title="Click here to login to your account"type="submit" value="Login">登录</button> </form>具体范例可参考官方Formhelper: https://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
4.modify password:
$this->Auth->user('password')是出不来东西的,用debug显示为空.所以读取数据库hash后的password解决办法就是
$password = $this->User->find('all', array( 'fields' => array( 'password', ), 'conditions' => array('User.username' => $this->Auth->user('username')), ) );这里我的'username'是users表的主键.
然后进行判断,当前输入的password hash过后和数据库里存的hash比较,如果一样,就输出true然后进行修改密码的操作.
想的不错,估计咱们新手想法都这样.
我的数据库里保存的'123456'对应的hash值和修改密码时输入的hash值得到的结果不一样!!!
没关系,cakephp为我们准备了专门的比较方法check(): https://api.cakephp.org/2.6/class-BlowfishPasswordHasher.html
$passwordHasher = new BlowfishPasswordHasher(); if(!$passwordHasher->check($current_password,$password[0]['User']['password'])){ $this->Flash->error('原密码不正确'); return false; }
5.如果想在一个controller里用2个表:
在你的controller里写下这样的话
public $uses = array('Teacher', 'Course');
6.想让外界用户不登陆就能访问的页面:
Appcontroller里beforeFilter方法加上:
$this->Auth->allow('index');
7.bootstrap模板怎么用?
很简单,把你要固定的部分(top or left)放到/view/layout/default.ctp/ 下,剩下center的部分
<div id="wrap" ><div id="top"> </div> <!-- /#top --> <div id="left"></div><!-- /#left --> <?php echo $content_for_layout ?><br/> </div>
8.设计编辑或删除功能要注意什么?
按照官方的话,如果用get方法的话容易让别人爬网站时候把东西误删,所以这些功能一定要post方法,且在相应controller添加如下语句
if ($this->request->is('get')) { throw new MethodNotAllowedException(); }如有错误,还请指正