从同一控制器内调用codeigniter方法


Calling codeigniter method from within same controller

我的控制器类中有一个方法,它正在执行一些操作等。现在我需要从生成PDF的同一控制器中调用另一个方法。

但是当我调用第二个方法时,第一个方法不完整?

我这样调用第二个方法,所以这行下面的任何东西都不完整:

$this->generate_certificate($course_id);

我是不是错过了什么?我需要以一种不会阻止第一个方法继续的方式调用它吗。

第一种方法,相关的位|:

else if($progress == 'c') {
                // the course is complete and the user has pressed the complete button
                // on the final page of the course
                // set course as complete in the db, set completed date, set due to next due
                // as defined in course date
                // get todays date
                $todays_date = date("Y-m-d");
                // add specified months on for next due
                $months = $course_object[0]->renewal_period;
                $due_date =  date('Y-m-d', strtotime($todays_date . ' + ' . $months .' months'));
                $training_data = array();
                $training_data['user_id'] = $user_id;
                $training_data['course_id'] = $course_id;
                $training_data['due'] = $due_date;
                $this->training_model->set_course_complete($training_data);
                $this->generate_certificate($course_id);
                //certificate generated, redirect to training dashboard
                $view_data = array(
                    'page_title'    => 'Training Dashboard',
                    'user_courses'  => $this->training_model->get_user_courses($user_id),
                );
                $this->session->set_flashdata('msg', 'You have successfully completed this course!');
                $this->load->view('training/training_dashboard',$view_data);

            } 

生成PDF的方法:

private function generate_certificate($course_id) {
    $this->load->model('org_model');
    $this->load->model('user_model');
    // get todays date
    $todays_date = date('d F Y');
    // get currently logged in user details
    $user_id = $this->session->userdata('user_id');
    $user_object = $this->user_model->get_user($user_id);
    $users_name =  $user_object[0]->first_name . " " . $user_object[0]->last_name;
    // course data
    $course_object = $this->training_model->get_course_data($course_id);
    // get org name
    $org_id = $this->session->userdata('org_id');
    $org_name_object = $this->org_model->get_org_name($org_id);
    $org_name = $org_name_object[0]->org_name;
    $html=" // I've removed this as its large, but works perfectly // ";
    //this the the PDF filename that user will get to download
    $pdfFilePath = "Certificate.pdf";
    //load mPDF library
    $this->load->library('m_pdf');
    //generate the PDF from the given html
    $this->m_pdf->pdf->WriteHTML($html);
    //download it.
    $this->m_pdf->pdf->Output($pdfFilePath, "D");
}

您应该在生成PDF的模型中创建一个方法。您可以在模型中调用其他模型方法,因此可以将generate_certificate方法放置在新模型中。然后在控制器中加载该模型并调用该方法。

这不会破坏处决。

此外,如果您只需要一个方法来生成PDF,也可以调用模型方法。