cakephp 中的controller 變量

黄舟
發布: 2023-03-03 16:24:02
原創
1040 人瀏覽過

Controller 變量

操作Controller裡的少量變量,可以讓你最大程度的使用Cake的額外功能:

$name

PHP4沒有把當前的駝峰格式的類名給我們。如果你有問題,可以使用此變數來設定正確的駝峰格式的類別名稱。

$uses

你的Controller是否使用多個model呢? FragglesController會自動載入$this->Fraggle,但如果你也想訪問$this->Smurf,試試將下面的東東加到你的controller中:

var $uses = array('Fraggle','Smurf');
登入後複製

請注意你是如何在$use數組中包含Fraggle model的,雖然在之前它也自動可用。

$helpers

使用本變數可以讓controller把 helper載入到它的view中去。 HTML helper會自動加載,但你可以使用本變數指定其他的:

var $helpers = array('Html','Ajax','Javascript');
登入後複製

記住,如果你打算用它的話,你需要在$helpers數組中包含HtmlHelper。一般它是缺省可用的,但是如果你定義了沒有它的$helpers,在你的view中你會得到錯誤訊息。

$layout

將本變數設定為你想在controller中使用的佈局名稱。

$autoRender

將本變數設為false,會自動停止action的render。

$beforeFilter

如果你想讓你的一點點代碼在每次的action調用中都運行(和任何動作運行之前),使用$beforeFilter吧。此東西對訪問控制來說真的非常好-你可以在任何動作發生前檢查使用者的權限。將此變數設為一個包含controller 動作的陣列。可以如下運行:

class ProductsController extends AppController
{
    var $beforeFilter = array('checkAccess');
 
    function checkAccess()
    {
        //Logic to check user identity and access would go here....
    }
 
    function index()
    {
        //When this action is called, checkAccess() is called first.
    }
}
登入後複製

$components

與$helpers和$uses一樣。此變數用來載入你需要的元件:

var $components = array(&#39;acl&#39;);<!--[if !supportFootnotes]-->[2]<!--[endif]-->
登入後複製

以上就是cakephp 中的controller 變數的內容,更多相關內容請關注PHP中文網(www.php.cn)! 


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!