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><?php echo $this->Session->flash();?></p>

或者

$this->Session->write('Message.auth',array('message'=>__('The user has been saved.',true),'element'=>'','params'=>array())); <p><?php echo $this->Session->flash('auth');?></p>

6. session设置

可参考:https://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 变量值。

renew

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

write(string $name, mixed $value);

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

error

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

7. 表单

<?php echo $this->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')); ?> <ul> <li> <div>下拉单选(编辑页面会自动判断选中)</div> <div> <?php echo $this->Form->input('type',array('type'=>'select' ,'class'=>'ipt','options' => array(0=>'文章',1=>'专题', 2=>'图组')));?> </div> </li> <li> <div>多选</div> <div> <?php echo $this->Form->input('pushtype', array('type'=>'select', 'options' => $pushtype,//所有选项 'multiple'=>'checkbox', 'selected' => $pushtypes,//选中的项 )); ?> </div> </li> </ul> <div> <button type="submit"><span>保存</span></button> <button><span>取消</span></button> </div> <?php echo $this->Form->end();?>

8. 日志$this->log();

在controller直接调用:

$this->log('Something brok2',LOG_DEBUG);

或view里调用:

<?php $this->log('Something brok2',LOG_DEBUG); ?>

日志的类型大致有以下几种:

$levels = array( LOG_WARNING=> 'warning', LOG_NOTICE=> 'notice', LOG_INFO=> 'info', LOG_DEBUG=> 'debug', LOG_ERR=> 'error', LOG_ERROR=> 'error' );

日志文件都保存在/app/tmp/logs目录。

在/app/config/core.php文件中有日志的配置选项:

define('LOG_ERROR', 2);

9. 渲染路径

echo APP . 'webroot' . DS; //D:\wamp\www\cakephp\app\webroot\ echo APP . 'webroot' ; D:\wamp\www\cakephp\app\webroot

附:CakePHP必知的21条技巧

这篇文章可以说是CakePHP教程中最经典的了。虽然不是完整的手把手系列,但作者将自己使用CakePHP的经验总结了21条,这些尤其是对新手十分有用。

翻译时故意保留了一些CakePHP中特有的词语没有翻译,如controller、model等。相信学过CakePHP的人应该马上就能理解它们的意思吧。

另外,CakePHP的wiki已经失效,取而代之的是一个名为bakery的网站。原文中引用的wiki的链接也都已更新到了bakery上。

快速创建静态页面

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/caf4eff6458c4c00d9339c32475f046a.html