We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to send value from Controller to Controller

Hello community

First of all sorry for my bad english. I got some problem I don't know how to send value from controller to controller I have 2 controller that handle 2 tables in database this is CustomerController and some code from my application.

class CustomerController extends ControllerBase {
    public function createAction() {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                    "controller" => "customer",
                    "action"     => "index",
                ));
        }

        $customer = new Customer();
        $onu      = new Onu();

        $customer->cus_fname      = $this->request->getPost("cus_fname");
        $customer->cus_lname      = $this->request->getPost("cus_lname");
        $customer->cus_addr       = $this->request->getPost("cus_addr");
        $customer->cus_tel        = $this->request->getPost("cus_tel");
        $customer->cus_circuit_no = $this->request->getPost("cus_circuit_no");
        $customer->cus_fdf        = $this->request->getPost("cus_fdf");
        $customer->cus_sdp        = $this->request->getPost("cus_sdp");
        $customer->cus_odp        = $this->request->getPost("cus_odp");
        $customer->gpon_id        = $this->request->getPost("gpon_id");

        if (!$customer->save()) {
            foreach ($customer->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                    "controller" => "customer",
                    "action"     => "test",
                ));
        }

        $this->dispatcher->forward(array(
                "controller"      => "onu",
                "action"          => "savecustomer",
                "params"          => array(
                    'onu_type'       => $this->request->getPost("onu_type"),
                    'onu_mac'        => $this->request->getPost("onu_mac"),
                    'cus_circuit_no' => $this->request->getPost("cus_circuit_no"),
                    'pon_id'         => $this->request->getPost("gpon_id"),
                )
            )
        );

        $this->flash->success("customer was created successfully");

        return $this->dispatcher->forward(array(
                "controller" => "customer",
                "action"     => "test",
            ));
    }

I want to send value from createAction In CustomerController to savecustomerAction in OnuController

class OnuController extends ControllerBase {

    public function savecustomerAction($params = array()) {

        $onu            = new Onu();
        $onu_type       = $params['onu_type'];
        $onu_mac        = $params['onu_mac'];
        $cus_circuit_no = $params['cus_circuit_no'];
        $pon_id         = $params['pon_id'];

      if (!$onu->save()) {
                  foreach ($onu->getMessages() as $message) {
                      $this->flash->error($message);
                  }

}

I want to save it to my database too. Please explain how to save it and how to pass value . thank you so much

The easier way to passing value between two controller, you may using SESSION.



11.0k
Accepted
answer
edited Nov '14

thank you so much tonywonghk. now I found the way to resolv this problem. I change my createAction in CustomerController with this code and now it work fine. :)

    /**
     * Creates a new customer
     */
    public function createAction() {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                    "controller" => "customer",
                    "action"     => "index",
                ));
        }

        $customer = new Customer();
        $onu      = new Onu();

        $customer->cus_fname      = $this->request->getPost("cus_fname");
        $customer->cus_lname      = $this->request->getPost("cus_lname");
        $customer->cus_addr       = $this->request->getPost("cus_addr");
        $customer->cus_tel        = $this->request->getPost("cus_tel");
        $customer->cus_circuit_no = $this->request->getPost("cus_circuit_no");
        $customer->cus_fdf        = $this->request->getPost("cus_fdf");
        $customer->cus_sdp        = $this->request->getPost("cus_sdp");
        $customer->cus_odp        = $this->request->getPost("cus_odp");
        $customer->gpon_id        = $this->request->getPost("gpon_id");

        if (!$customer->save()) {
            foreach ($customer->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                    "controller" => "customer",
                    "action"     => "test",
                ));
        }

        /*      $this->dispatcher->forward(array(
        "controller"      => "onu",
        "action"          => "savecustomer",
        "params"          => array(
        'onu_type'       => $this->request->getPost("onu_type"),
        'onu_mac'        => $this->request->getPost("onu_mac"),
        'cus_circuit_no' => $this->request->getPost("cus_circuit_no"),
        'pon_id'         => $this->request->getPost("gpon_id"),
        )
        )
        );*/

        $onu->onu_type       = $this->request->getPost("onu_type");
        $onu->onu_mac        = $this->request->getPost("onu_mac");
        $onu->cus_circuit_no = $this->request->getPost("onu_mac");
        $onu->pon_id         = $this->request->getPost("gpon_id");
        $onu->save();

        $this->flash->success("customer was created successfully");

        return $this->dispatcher->forward(array(
                "controller" => "customer",
                "action"     => "test",
            ));
    }

thank you phalcon framework. Your are the great PHP Framework !