[英]Cakephp calling function from view
我有以下功能:
public function make_order($id = null){
if($this->request->is('post')){
if(isset($id)){
$single_product = $this->Product->find('first', array('Product.id' => $id));
$this->placeOrder($single_product);
}else{
$product_array = $_SESSION['basket'];
foreach($product_array as $product){
$this->placeOrder($product);
}
}
}
}
private function placeOrder($product){
$order_array = array();
$order_array['Order']['Product_id'] = $product['Product']['id'];
$order_array['Order']['lejer_id'] = $this->userid;
$order_array['Order']['udlejer_id'] = $product['users_id'];
$this->Order->add($order_array);
}
現在,這兩個功能沒有“連接”到視圖,但是我仍然需要從另一個視圖中調用它們
為此,我嘗試了以下方法:
<?php echo $this->Html->link(__('Bestil'), array('action' => 'make_order')); ?>
但是這引發了一個錯誤,說它找不到與make_order
相匹配的視圖,這是有充分的理由的(我創建了一個視圖,但我不打算創建一個視圖)
我的問題是如何在我的視圖中調用並執行此功能?
在make_order
函數的最后,您要么需要:
a)指定要渲染的視圖文件,或b)重定向到確實具有要渲染的視圖文件的其他控制器和/或操作。
a)看起來像這樣:
$this->render('some_other_view_file');
b)可能看起來像這樣(注意:設置即顯消息是可選的)
$this->Session->setFlash(__('Your order was placed'));
$this->redirect(array('controller' => 'some_controller', 'action' => 'some_action'));
您可以通過設置$this->autoRender = false;
來關閉自動渲染$this->autoRender = false;
在控制器的操作中(在這種情況下為make_order()
)。 這樣,您就不需要查看文件,並且可以輸出所需的任何內容。
問題在於屏幕上不會呈現任何內容。 因此,我的建議是讓您的“鏈接”僅通過AJAX調用controller :: action。 如果在您的情況下無法做到這一點,則必須在make_order()
方法中呈現視圖,或重定向到將呈現視圖的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.