cakephp常见知识点汇总

本文实例总结了cakephp常见知识点。分享给大家供大家参考,具体如下:

1. 调用其他控制器的模板,重定向

方法一:

在此调用/views/tasks/tasks下的hello.ctp模板

viewPath = ‘tasks’;
$this -> render(‘hello’);

方法二(带参):

$this->redirect(array(‘controller’=>’users’,’action’=>’welcome’,urlencode($this->data[‘姓名’].’haha’)));

2. 查询

直接使用sql:

PostContent->query(“select * from user”);
find():
$clue = $this->clue->find(‘all’,array(
‘fields’ =>array(
‘id’,’title’,’content’
),’order’ => ‘id ASC’,’conditions’ => array(‘id’ => ‘1’),)
);

find的参数,第一个可以是all、first、count,第二个参数为一数组,数组的key可以是:conditions、fields、order、limit、offset、joins。

添加:

clue->create();
$this->clue->save($this->data);

修改:

clue->create();
$this->clue->save($this->data);

删除:

clue->delete($id)

3. 不需要公共样式时

layout = false;

不用渲染任何view

autoRender = false;

4. 定义公共的方法/类

方法一:

可以在/app/Controller/AppController.php中定义公共的方法

调用

test();

方法二:

在/app/controllers/components中创建UtillComponent.php

调用:

Utill->juanstr($digit1);

5. 定义提示信息

Session->setFlash(__(‘The user has been saved’));

<p class=”wrong”><?php echo $this->Session->flash();?>

或者

Session->write(‘Message.auth’,array(‘message’=>__(‘The user has been saved.’,true),’element’=>”,’params’=>array()));

<p class=”wrong”><?php echo $this->Session->flash(‘auth’);?>

6. session设置

可参考:

check(string $name);

检查Session中是否已有$name为键值的数据项.

del(string $name);
delete(string $name);

删除$name 指定的 Session 变量。

valid当Session有效时返回true,最好在read()操作前用它来确定你要访问的会话是否确实有效.

read(string $name);

返回 $name 变量值。

renew

通过创建新的seesion ID,删除原有的ID,将原有Session中信息更新到新的Session中。

write(string $name,mixed $value);

将变量 $name,$value写入会话.

error

返回最近由 Cake Session Component 产生的错误,常用于调试。

7. 表单

Form->create(‘Subject’,array(
‘type’ => ‘post’,’inputDefaults’=>array(
‘div’=>false,’label’=>false
),’url’=>array(
‘controller’=>’subjects’,’action’=>’edit’
),’onsubmit’=>’return validateCallback(this,dialogAjaxDone);’ //提交前验证
)
);
echo $this->Form->input(‘id’,array(‘type’=>’hidden’));
echo $this->Form->input(‘uid’,array(‘type’=>’hidden’));
?>

    Form->input(‘type’,array(‘type’=>’select’,’class’=>’ipt’,’options’ => array(0=>’文章’,1=>’专题’,2=>’图组’)));?>

  • Form->input(‘pushtype’,’options’ => $pushtype,//所有选项
    ‘multiple’=>’checkbox’,’selected’ => $pushtypes,//选中的项
    ));
    ?>