2011-02-01 85 views
3

基本上,我想知道的是,如果您可以使用Auth組件來允許某些擴展(JSON/HTML)?CakePHP Auth允許JSON擴展

基本上,可以說我們有一個動作,動作就是索引。在這個動作中,我們所做的只是列表作者(用戶)。所以url是http://somewebsite.com/authors/index。如果我們訪問該網址,內容類型將爲HTML,應限制爲登錄用戶(管理員),以便他們可以擁有編輯/刪除按鈕。但是,當您將.json擴展名放在末尾時,我們也會使用此操作來呈現json,因此url將爲http://somewebsite.com/authors/index.json。在這種情況下,您不需要登錄,因爲您只想訪問該信息。

那麼,Auth組件是否可以允許某些擴展,這是最好的方法嗎?

感謝和乾杯!

回答

3

沿着這些線路的東西應該工作(包括明確「解鎖」只有特定的方法):

public function beforeFilter() { 
    $methods = array('index', 'foo', 'bar'); 

    // please forgive the terrible indentation 
    if (in_array($this->action, $methods) && 
     isset($this->params['ext']) && $this->params['ext'] == 'json' 
    ) { 
     $this->Auth->allow($this->action); 
    } 
} 
+0

你會怎麼做,如果有不是一個「分機」 PARAM? – alvincrespo 2011-02-02 04:36:53