2007年5月15日 星期二

CakePHP使用手冊-要求處理元件

轉貼自 http://www.ezluk.org/

第一節



引言


RequestHandler component在Cake中被用來判定傳回的HTTP reqeust。
你可以用它通知controller有Ajax request,取得遠端client的IP位置和request種類,或去除不要的資料。
要使用RequestHandler component,得確認controller的$components陣列是否包含了這個component。



class ThingsController extends AppController

{

    var $components = array('RequestHandler');



    // ...

}


第二節



取得client/Request訊息


就讓我們開門見山,直接看函式說明:





  • accepts

  • string $type



依$type值傳回client接受的content-type。
如果$type等於null或沒有傳值,會傳回內含client能接受的所有content-type的陣列。
如果傳了一個字串過去,則會將$type拿去和content-type表(參考setContent())比對,若client可以接受這個種類時傳回true。
如果$type是一個陣列,每個字串會分別比對,如果其中一個能接受就傳回true。例如:



class PostsController extends AppController

{

    var $components = array('RequestHandler');



    function beforeFilter ()

    {

        if ($this->RequestHandler->accepts('html'))

        {

            // 當client接受HTML(text/html)時才執行這裡的程式

        }

        elseif ($this->RequestHandler->accepts('rss'))

        {

            // 當client接受RSS(application/rss+xml)時才執行這裡的程式

        }

        elseif ($this->RequestHandler->accepts('atom'))

        {

            // 當client接受atom(application/atom+xml)時才執行這裡的程式

        }

        elseif ($this->RequestHandler->accepts('xml'))

        {

            // 當client接受XML(application/xml 或 text/xml)時才執行這裡的程式

        }



        if ($this->RequestHandler->accepts(array('xml', 'rss', 'atom')))

        {

            // 當client接受XML,RSS,ATOM其中之一時才執行這裡的程式

        }

    }

}





  • getAjaxVersion




若使用Prototype JS 函式庫,它會在Ajax request上放置一段特殊表頭資料。
此函式可以傳回檔頭內記錄的Protytype版本資訊。





  • getClientIP




傳回client的IP位置。





  • getReferrer




傳回最出發出這個request的server名稱。





  • isAjax




如果目前的request是XMLHttpRequest就傳回true。





  • isAtom




如果client能接受Atom格式的內容則傳回true。





  • isDelete




若目前的request的'REQUEST_METHOD'等於DELETE則傳回true。





  • isGet




若目前的request的'REQUEST_METHOD'等於Get則傳回true





  • isMobile




如果HTTP_USER_AGENT說明了這是個手機的web流覽器則傳回true。





  • isPost




若目前的request的'REQUEST_METHOD'等於POST則傳回true





  • isPut




若目前的request的'REQUEST_METHOD'等於PUT則傳回true





  • isRss




若client能接受RSS格式的內容(application/rss+xml),則傳回true。





  • isXml




若client能接受XML格式的內容(application/xml 或 text/xml),則傳回true。





  • setContent

  • string $name

  • string $type



在content-type的別名表中加入一個選項,與accepts()和prefers()配合使用。
$name是別名名稱,$type則是MIME型別。
內建的幾種格式為:



// Name     => Type
'js' => 'text/javascript',
'css' => 'text/css',
'html' => 'text/html',
'form' => 'application/x-www-form-urlencoded',
'file' => 'multipart/form-data',
'xhtml' => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),
'xml' => array('application/xml', 'text/xml'),
'rss' => 'application/rss+xml',
'atom' => 'application/atom+xml'



第三節



精簡資料


有時候我們會想刪除request或輸入的資料內部分內容。
下列的RequestHandler函式可以做這類的事。





  • stripAll

  • string $str



刪除$str內所有的空白,影像和script(就等於同時呼叫stripWhitespace(),stripImages()和stripScripts())。





  • stripImages

  • string $str



刪除$str裡的影像。





  • stripScripts

  • string $str



移除$str裡所有<script> 和<style> 相關的標籤。





  • stripTags

  • string $str

  • string $tag1

  • string $tag2...



移除$str裡$tag1,$tag2等參數指定的標籤。


$someString = '<font color="#FF0000"><bold>Foo</bold></font> <em>Bar</em>';



echo $this->RequestHandler->stripTags($someString, 'font', 'bold');



// 輸出: Foo <em>Bar</em>




  • stripWhiteSpace

  • string $str



移除$str內的空白字元。



第四節



其他好用的函式


當應用程式使用到AJAX技術時,RequestHandler component更會顯得特別好用。
setAjax()函式可以自動偵測AJAX的request,然後將controller的layout轉換成AJAX layout。
這樣的好處在於你可以做一個小視窗,在收到AJAX request時變大二倍。


// list.thtml

<ul>

<? foreach ($things as $thing):?>

<li><?php echo $thing;?></li>

<?endforeach;?>

</ul>



//-------------------------------------------------------------



//The list action of my ThingsController:

function list()

{

    $this->RequestHandler->setAjax($this);

    $this->set('things', $this->Thing->findAll());

}

 

當一般的流覽器呼叫/things/list時,沒排序的列表會被畫在應用程式預設的layout上。
但如果這是一個AJAX的rquest,列表就會自動被畫在AJAX layout上。


沒有留言:

網誌存檔

關於我自己

Aspire freedom , Hope to do Soming make self complete ~