如何在codeigniter中使用重定向传递数据

时间:2018-04-22 08:58:33

标签: php codeigniter

我是CodeIgniter的新手。我正在尝试实施stripe payment gateway integration。支付成功状态正常。我试图在用户失败时显示给用户。但是将故障数据传递给我的视图是行不通的。以下是我的代码。

 public function stripepay()
    {
    // some post variables
    try {
    require_once APPPATH."third_party/stripe/init.php";
    //set api key
    $stripe = array(
    "secret_key"      => "sk_test_gSev8A4OJf0F3BXXXXXXXX",
    "publishable_key" => "pk_test_XZZxorO1rYsZTJXXXXXXXX"
    );
    \Stripe\Stripe::setApiKey($stripe['secret_key']);
    // some more code
    }catch ( Stripe\Error\Base $e )
    {
    // Code to do something with the $e exception object when an error occurs.
    $body = $e->getJsonBody();
    $err  = $body['error'];
    $data['failure_response'] = $err;
    $data['response_status'] = $e->getHttpStatus();
    $where = array('id' => $this->session->userdata('id'));
    $payment_info = $this->baseM->getOneRowData('users', $where);
    // this redirct is not working 
    //redirect('service/failure_402'.$data);
    }
    }

当它进入此catch块时,我试图重定向到failue402视图。

public function failure_402($data)
{
$where = array('id' => $this->session->userdata('id'));
$payment_info = $this->baseM->getOneRowData('users', $where);
$this->load->view('page_layout/header', $data);
$this->load->view('service/failure_402',$data);
$this->load->view('page_layout/footer', $data); 
}

In the above stripePay() function 
// this redirect is not working 
redirect('service/failure_402'.$data); 
is not working.

请指导我在哪里犯错误。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您可以使用codeigniter会话的set_flashdata()方法

{

$body = $e->getJsonBody();
$err  = $body['error'];
$data['failure_response'] = $err;
$data['response_status'] = $e->getHttpStatus();
$where = array('id' => $this->session->userdata('id'));
$payment_info = $this->baseM->getOneRowData('users', $where);

/* EITHER */
$this->session->set_flashdata('failure_response' ,$err);
$this->session->set_flashdata('failure_response' ,$e->getHttpStatus());
/* OR */
 $this->session->set_flashdata($data);

 redirect('service/failure_402');
}

falure_402()

public function failure_402()
{
   print_r($this->session->flashdata());
   /*OR this way */
  echo $this->session->flashdata('failure_response')
}

更多信息:https://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata