国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial Summary of cakephp knowledge points

Summary of cakephp knowledge points

Feb 25, 2017 pm 04:20 PM

The examples in this article summarize common knowledge points of cakephp. Share it with everyone for your reference, the details are as follows:

1. Call the template of other controllers and redirect

Method 1:

Call the hello.ctp template under /views/tasks/tasks here

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

Method 2 (with parameters):

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

2. Query

directly using 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 parameters, the first one can It is all, first, and count. The second parameter is an array. The key of the array can be: conditions, fields, order, limit, offset, and joins.

Add:

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

Modify:

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

Delete:

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

3. When no public style is required

$this->layout = false;

No need to render any view

$this->autoRender = false;

4. Define public methods/classes

Method 1:

You can define public methods in /app/Controller/AppController.php

Call

$this->test();

Method 2:

Create UtillComponent.php in /app/controllers/components

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

Call:

var $components = array(&#39;Utill&#39;);
$digit1 = $this->Utill->juanstr($digit1);

5. Define prompt message

$this->Session->setFlash(__(&#39;The user has been saved&#39;));

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

or

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

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

6. Session settings

Please refer to: http://m.miracleart.cn/

check(string $name);

Check whether there is a data item with $name as the key value in the Session.

del(string $name);<br/>delete(string $name);

Delete the Session variable specified by $name.

valid returns true when the Session is valid. It is best to use it before the read() operation to determine whether the session you want to access is indeed valid.

read(string $name) ;

Return $name variable value.

renew

By creating a new seesion ID, deleting the original ID, and updating the information in the original Session to the new Session.

write(string $name, mixed $value);

Write the variables $name, $value into the session.

error

Returns the most recent error generated by Cake Session Component, often used for debugging.

7. Form

<?php echo $this->Form->create(&#39;Subject&#39;,array(
  &#39;type&#39; => &#39;post&#39;,
  &#39;inputDefaults&#39;=>array(
    &#39;p&#39;=>false,
    &#39;label&#39;=>false
    ),
  &#39;url&#39;=>array(
      &#39;controller&#39;=>&#39;subjects&#39;,
      &#39;action&#39;=>&#39;edit&#39;
    ),
  &#39;onsubmit&#39;=>&#39;return validateCallback(this, dialogAjaxDone);&#39; //提交前驗證
  )
);
echo $this->Form->input(&#39;id&#39;,array(&#39;type&#39;=>&#39;hidden&#39;));
echo $this->Form->input(&#39;uid&#39;,array(&#39;type&#39;=>&#39;hidden&#39;));
?>
<ul class="usr_info_basic">
<li>
  <p class="ti">下拉單選(編輯頁面會自動判斷選中)</p>
  <p class="ce">
<?php echo $this->Form->input(&#39;type&#39;,array(&#39;type&#39;=>&#39;select&#39; ,&#39;class&#39;=>&#39;ipt&#39;,&#39;options&#39; => array(0=>&#39;文章&#39;,1=>&#39;專題&#39;, 2=>&#39;圖組&#39;)));?>
  </p>
</li>
<li>
  <p class="ti">多選</p>
  <p class="ce">
<?php
  echo $this->Form->input(&#39;pushtype&#39;, array(&#39;type&#39;=>&#39;select&#39;,
    &#39;options&#39; => $pushtype,//所有選項
    &#39;multiple&#39;=>&#39;checkbox&#39;,
    &#39;selected&#39; => $pushtypes,//選中的項
));
?>
  </p>
</li>
</ul>
<p class="btns_3">
  <button class="btn3" type="submit"><span>保存</span></button>
  <button class="btn3 btn3_1 close"><span>取消</span></button>
</p>
<?php echo $this->Form->end();?>

##8. Log$this->log();

Call directly in the controller:

$this->log(&#39;Something brok2&#39;,LOG_DEBUG);

or call in the view:

The types of logs are roughly as follows:

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

Log files are saved in the /app/tmp/logs directory.

There are log configuration options in the /app/config/core.php file:

define(&#39;LOG_ERROR&#39;, 2);

9. Rendering path

echo APP . &#39;webroot&#39; . DS;
//D:\wamp\www\cakephp\app\webroot\
echo APP . &#39;webroot&#39; ;
D:\wamp\www\cakephp\app\webroot

Attachment: 21 tips you must know about CakePHP

This article can be said to be a CakePHP tutorial The most classic among them. Although it is not a complete step-by-step series, the author summarized his experience in using CakePHP in 21 items, which are very useful especially for novices.

During the translation, some words unique to CakePHP were intentionally left untranslated, such as controller, model, etc. I believe that people who have learned CakePHP should be able to understand their meaning immediately.

In addition, CakePHP's wiki has expired and was replaced by a website called bakery. The links to the wiki cited in the original article have also been updated to the bakery.

Quickly create static pages

I want to create several pages that only contain static data, use the default layout, and do not require any model. Initially I tried to create a controller and define an action for each static page. But this method is clumsy and not suitable for quickly creating static pages.

In fact, you can do it by using the pages controller - as long as you create a view under the views/pages folder, you can access it through /pages. For example, I created /views/pages/matt.thtml, which can be accessed through http://m.miracleart.cn/.

Change the title of the static page

If you want to change the page title when using the pages controller, just add the following code to the view:

<? $this->pageTitle = &#39;Title of your page.&#39;; ?>

Send data to layout in a static page

If you need to pass data to layout (for example, indicating which part of the navigation bar should be highlighted variable), you can add the following code to the view:

<? $this->_viewVars[&#39;somedata&#39;] = array(&#39;some&#39;,&#39;data&#39;); ?>

This array can be accessed through $somedata in the layout.

Quickly create background management

If you need to create a background management program and want all management actions to be located in a specific folder, then open config/core. php and remove the comment from the following line:

define(&#39;CAKE_ADMIN&#39;, &#39;admin&#39;);

這樣所有以"admin_"開頭的action都可以通過 /admin/yourcontroller/youraction 來訪問。例如,如果在posts controller中創(chuàng)建了名為"admin_add"的action,那么可以通過 www.example.com/admin/posts/add 訪問這個action。這樣就可以方便地為admin目錄設(shè)置密碼以避免他人隨意訪問。

查看后臺執(zhí)行的SQL語句

只需改變config/core.php中的DEBUG常量,即可看到后臺執(zhí)行的SQL語句。0為產(chǎn)品級,1為開發(fā)級,2為完整調(diào)試SQL,3為完整調(diào)試SQL并顯示對象數(shù)據(jù)。我通常將DEBUG設(shè)置為2,這樣每頁的底部會顯示出一個包含SQL調(diào)試信息的表格。

如果頁面底部添加的表格會破壞頁面布局(特別是使用Ajax獲取頁面并顯示到頁面中間而不是底部時),你可以在CSS中添加以下代碼以隱藏調(diào)試信息:

#cakeSqlLog { display: none; }

這樣既能保持頁面布局,又可以通過查看源代碼來看到調(diào)試信息。當(dāng)然最后發(fā)布網(wǎng)站時別忘了將調(diào)試級別改回0。

獲取豐富的開發(fā)文檔

別總是盯著手冊。wiki和API也是無價之寶。wiki中的開發(fā)指南十分有用,而API文檔初看起來比較難,但你很快就會發(fā)現(xiàn)這里的信息對你創(chuàng)建CakePHP網(wǎng)站十分重要。

使用bake.php

Bake是個命令行PHP腳本,可以根據(jù)數(shù)據(jù)庫自動生成model、controller和view。在開發(fā)的最初階段,我強(qiáng)烈推薦使用scaffolding讓你的原型程序跑起來。但如果你清楚地知道scaffolding不合適,我推薦你使用bake。bake會生成所有的文件并保存到磁盤上,以便你隨意修改。這樣能節(jié)省創(chuàng)建關(guān)聯(lián)、view、基本的CRUD crollder操作的重復(fù)工作。

(譯者注:CRUD - Create, Read, Update, Delete,數(shù)據(jù)庫應(yīng)用的四種基本操作,即"增刪查改"。)

bake很方便。你只需在數(shù)據(jù)庫中建立一個表,然后到 /cake/scripts/ 目錄下執(zhí)行php bake.php 即可。

如果你通過交互方式來運(yùn)行bake,它會分幾步提示你創(chuàng)建model、controller和view。創(chuàng)建結(jié)束之后,我通常會閱讀所有生成的代碼并做必要的修改。

發(fā)布程序時注意權(quán)限

有一次我在發(fā)布程序時,將整個cake目錄打包然后用scp上傳到了服務(wù)器上。只要一關(guān)閉調(diào)試信息,就會出現(xiàn)錯誤——數(shù)據(jù)庫調(diào)用無法返回任何數(shù)據(jù)。我一籌莫展,因為我必須通過調(diào)試信息才能調(diào)試問題。后來有人告訴我,/app/tmp應(yīng)當(dāng)對apache可寫。將權(quán)限改為777之后問題就解決了。

復(fù)雜model驗證

我需要進(jìn)行更復(fù)雜的驗證,而不僅僅是驗證輸入框非空或者符合某個正則表達(dá)式這樣的簡單驗證。例如,我要驗證用戶注冊時使用的郵件地址是否已被使用。在wiki中我找到了這篇關(guān)于高級驗證的文章,其中提到了一些十分有用的高級驗證方法。

記錄錯誤日志

$this->log(&#39;Something broke&#39;);

這樣可以將錯誤記錄到 /tmp/logs/ 中(我最初以為會記錄到apache的錯誤日志中)。

讓controller使用其他model

如果你的controller需要調(diào)用來自不同model的數(shù)據(jù),只要在controller開頭使用如下代碼:

class yourController extends AppController {
 var $uses = array(&#39;Post&#39;,&#39;User&#39;);
}

這樣controller就能訪問Post和User model了。

創(chuàng)建不使用數(shù)據(jù)庫表的model

我需要創(chuàng)建一個不使用任何表的model。例如,我想通過$validate數(shù)組方便底驗證輸入數(shù)據(jù),保持model邏輯的正確性。但創(chuàng)建model時對應(yīng)的表不存在,CakePHP就會報錯。通過在model中加入以下代碼可以解決這個問題:

var $useTable = false;

你也可以通過這種方法改變model對應(yīng)的表名。

var $useTable = &#39;some_table&#39;;

重定向之后記得exit()

對于有經(jīng)驗的人來說這應(yīng)當(dāng)是理所當(dāng)然的事兒,調(diào)用 $this->redirect() 之后,剩下的代碼如果不想運(yùn)行要exit()。我也這樣做,但以前曾經(jīng)認(rèn)為 $this->redirect() 會為我調(diào)用exit(實際上不會)。

高級model函數(shù)

翻翻API就能發(fā)現(xiàn)很多你不知道的非常有用的函數(shù)。我強(qiáng)烈推薦至少閱讀一遍 Model 類的參考手冊。下面是以前我沒注意到的幾個重要函數(shù):

generateList() - 主要用于生成選擇框(