cakephp常见知识点汇总

2019-05-02 00:23:04丽君

本文实例总结了cakephp常见知识点。,具体如下:

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

方法一:

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

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

方法二(带参):

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

2. 查询

直接使用sql:

$this->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。

添加:

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

修改:

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

删除:

$this->clue->delete($id)

3. 不需要公共样式时

$this->layout = false;

不用渲染任何view

$this->autoRender = false;

4. 定义公共的方法/类

方法一:

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

调用

$this->test();

方法二:

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

<?php
  class UtillComponent extends Object
  {
   function juanstr ($str) {
     return $str.'+juanstr';
   }
  }
?>

调用:

var $components = array('Utill');
$digit1 = $this->Utill->juanstr($digit1);

5. 定义提示信息

$this->Session->setFlash(__('The user has been saved'));

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

或者

$this->Session->write('Message.auth',array('message'=>__('The user has been saved.',true),'element'=>'','params'=>array()));

<p class="wrong"><?php echo $this->Session->flash('auth');?></p>

6. session设置

可参考://www.jb51.net/article/106557.htm

check(string $name);

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

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

删除$name 指定的 Session 变量。

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

read(string $name);

返回 $name 变量值。

相关文章 大家在看