Question

I have to make some design decision in my application using Codeigniter.

I have a method in controller that calls a library for creating PDF. Also I have some class that takes a number as an argument and returns string (number verbally ).

I would like to know what is the bestr practice to pass data between all this class. Is this a task of controller to call all libraries (between step2 and step 3) and provide all prepared data to a model that will create PDF. Or is this a task of Model itself to transform provided raw data by loading and calling class that converts number to string.

What would be the best solution in terms of loose coupling and modularity and clarity of the code.

This is a controller:

class Payu extends CI_Controller
{
     public function raport($task_id)
     {
           /* (step 1) Load necessarty models */
           $this->load->model('MTasks');
           $this->load->model('mpdfinvoice');

           /* (step 2) task details from DB */
           $task_details = $this->MTasks->getTaskDetails($task_id);

           /* (step 3) create PDF that will be send */
           $this->mpdfinvoice->pdf($task_details);

           /* (step 4) compose an email with attached pdf */
           $this->email->from($this->config->item('noreply_email'));
           $this->email->to($task_details['email']);
           $this->email->attach('invoiceJustCreated.pdf');
           $this->email->subject('opłaciłes to zlecenie');
           $message = 'some message goes here';
           $this->email->message($message);
           $this->email->send();


     }
}


 This is a model that creates PDF file (called by controller)

 class mpdfinvoice extends CI_Model
 {
     public function pdf($task_details)
     {
          /* (step 1) load necesary library and helper */
          $this->load->library(array('fpdf' ));
          $this->load->helper('file');

          /* (step 2) set PDF page configuration*/              
          $this->fpdf->AddPage();
          $this->fpdf->AddFont('arialpl','','arialpl.php');
          $this->fpdf->SetFont('arialpl','',16);

          /* (step 3) show data on PDF page */              
          $this->fpdf->cell('','',$task_details['payment_amount'] ,1);

          /*  I want to have "payment amount" verbally here 
              So Should I load and call the convert class here or
              should I have this data already prepared by the controller
              and only output it ? */

     }
 }
Was it helpful?

Solution

Try to look at controllers in codeigniter as your data glue. A controller retrieves data from clients, standardizes it and triggers the appropriate actions (redirect, triggering libraries, views, models and helpers while using the data provided.)

In your case just use a controller to retreive the data from your models or userinput. Then pass it to a library that creates the pdf. If the pdf creation is succesfull return true to let te controller know its a succes. Create a flashdata succes message and redirect to a page that outputs a view:

  1. User request: pdf/download
  2. Controller Pdf triggers the download method
  3. download triggers the models and pushes the data to a view
  4. The view with its data will be returned into a variable
  5. The $view variable will be pushed to a Pdf library
  6. trigger pdf create
  7. Pdf triggers pdf download (optional)
  8. Controller triggers redirect to pdf/overview

OTHER TIPS

I would suggest that the creation of the PDF or any other file and all the convertions between values to happen at the Model and then the Controller decide which data to fetch from the Model in order to pass it to the appropriate View and it will know how to display it.

Controllers should not be used to directly display(output) data or access database and do file creations.

Also Models should not ever be used to output data for any case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top