繁体   English   中英

从视图中调用Cakephp函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM