2011-10-09 74 views
2

使用CakePHP 1.3如何驗證非標準行動POST在蛋糕PHP 1.3

我有一個非標準操作名稱控制器 - 說:

class WidgetsController extends AppController { 

function modifyColor($id = null) { 
// Some code that modifies the background color of a widget 
} 

} 

和同伴觀點的看法/小部件/ modifyColor.ctp

的modifyColor模板帖的行動:

echo $this->Form->create('User',array('url' => array('controller' => 'widgets', 'action' => 'modifyColor'))); 

自從CakePHP安全組件試圖驗證表單 並且我希望能夠驗證POST,我在POST上得到了404。

我能得到這個工作的唯一方法似乎是要關閉POST驗證

if ($this->action == 'modifyColor') { 
    $this->Security->validatePost = false; 
} 

這似乎是一個不好解決。

如何在非標準操作上使用安全組件? 檢查allowedActions似乎並沒有工作 感謝 丹尼

回答我的問題。

答:沒有使用任何命名行動CakePHP的 使用CakePHP B.概念性錯誤與使用相同功能的形式GET和POST表單

在窗體GET我有這個沒問題:

if (empty($this->data)) { 
      $this->data = $this->Widget->read(null, $id); 
    } 

窗體本身有一些像這樣的代碼:

 echo $this->Form->input('id'); 
     echo $this->formView('Current color', 'CurrentColor'); 
     echo $this->formView('New color',  'NewColor'); 
     echo $this->formView('New background color', 'BackgrdColor'); 

這是罰款,但沒有這些字段出現在Widget模型 - 並且CakePHP安全組件將此解釋爲一種XSRF攻擊 - 因爲它在窗體中查找不屬於模型的字段。這就是爲什麼:

$this->Security->validatePost = false; 

解決了「問題」。

正確的解決方法是根本不填充$這個 - >數據與控制器動作模型和處理的POST字段分配:

function modcolor() { 
    $this->layout = 'app_ui_listview'; 
    if (!empty($this->data)) { 
      $id = $this->Session->read('Auth.User.id'); 
      $u = $this->Widget->read(null, $id); 
       // Assign fields from the form to the model.... 


    } 
} 
+0

應使用回聲$這 - >形式 - >創建( '工具',陣列( 'URL'=>陣列( '控制器'=>' widgets','action'=>'modifyColor')));而不是echo $ this-> Form-> create('User',array('url'=> array('controller'=>'widgets','action'=>'modifyColor'))); –

回答

0

問題是你試圖通過網址和你也試圖傳遞控制器的東西。

echo $this->Form->create('Widget', array('controller' => 'widgets', 'action' => 'modifyColor'));

echo $this->Form->create('Widget', array('url' => '/widgets/modifyColor'));

+0

在第一個版本中,'controller'鍵將被忽略(只有'action'鍵會有任何作用),第二個版本相當於問題中的代碼:'echo $ this-> Form-> create( 'User',array('url'=> array('controller'=>'widgets','action'=>'modifyColor')));'。 OR:不幸的是,你的回答不正確 - url鍵是有效的,在CakePHP中指定一個url作爲數組是正常的。儘管問題的地位如此,但這個問題已經解決 - 請參閱問題中的「回答我自己的問題」。 – AD7six