I have a use case where I do internal data computations using English system, due to various precomputed graph coefficients and other data already using the English system. But for output I need to convert some of my data to Metric system and leave some data in English system to be used for debugging purposes to follow the computation. (Debugging information will be there to stay long-term and not just used during development).

My question is this - where within the MVC do I make the value conversions, and why?

My current choices to do this conversion are in the Controller or in the View.

If I do the conversion inside the controller, I will essentially need to maintain two sets of variables (one for computations, one for to pass to View.

If I do conversion in View, my Controller will be leaner due to only knowing English system, but the View will acquire various conversion coefficients and conversion code and thus become heavier.

There is perhaps a third option - does conversion need to essentially become another layer between Controller and the View? If so where or how do I wire it in?

More Info on my specific situation

I have a PHP web application written in Zend Expressive. Controllers there are called Handlers.

class Handler // aka Controller
    private $renderer; // renders view template

    function handle($request)
         $system = $request->getUnitSystem(); //"English", "Metric"
         $page = $request->getPage(); //page number
         $data = $this->repository->getEnglishData(); //PHP array of English-valued data sets

         /* Conversions in Handler */
         $data[$page]['height'] = $this->valueConverter->feet_to_meters($data[$page]['height']);
         $data[$page]['height_unit'] = $system->isMetric() ? 'meters' : 'feet';

         $params = array(
             'paginator' => $this->paginator->getPaginator($data, $page),

         /* Send to View */
         return new HtmlResponse($this->renderer->render('show_data.phtml', $params));


So above I have the following players:

  • $system, which is the value of the unit system (English or Metric)
  • $valueConverter, which is a library of value conversion methods
  • $data, a PHP array with a set of of English-valued data

I use conversion in the Controller to update values to use correct Unit System, and I send those off into the View. View displays them as-is.

Extra Complication when to different systems are to be displayed

I also have a use case where I need to show debugging information on the screen in English units (show internal computations for debugging purposes) and show "Real" information in converted Metric units. So different unit systems may need to be displayed at the same time. How to structure my code to handle this use case?

  • 1
    Is the unit of measure a user preference or form field? Is so this sounds more like an issue of localization than anything else. – Greg Burghardt Jul 10 '18 at 22:06
  • It is a preference. Does localization mean I need to do this in Controller or View or does it not mean those things? localization is kind of abstract – Dennis Jul 11 '18 at 13:34
  • I think adding "localization" somewhere in your question might actually help get you the answer you are looking for. – Greg Burghardt Jul 11 '18 at 13:46

Perhaps a variant on option 3: make the model responsible for handling the conversion.

Say you have this:

class CalculationResultInEnglishUnits
    public decimal HeightInFeet { get; set; }
    public decimal WeightInPounds { get; set; }

and you need it to look like this:

class CalculationResultInMetricUnits
    public decimal HeightInMeters { get; set; }
    public decimal WeightInKillograms { get; set; }

You could add a conversion constructor (or factory method or helper or ...) to convert between the two. Something like this:

class CalculationResultInMetricUnits
    public decimal HeightInMeters { get; set; }
    public decimal WeightInKillograms { get; set; }

    public CalculationResultInMetricUnits(CalculationResultInEnglishUnits results)
        HeightInMeters = UnitConversionHelper.FeetToMeters(results.HeightInFeet);
        WeightInKillograms = UnitConversionHelper.PoundsToKillograms(results.WeightInPounds);

This way, neither the View or Controller need to care about the conversion. The model takes responsibility for maintaining consistent state and being the way of passing data between the controller and view (and in this case, translating it to the correct form).

  • I may have some trouble. What i have is a PHP web application with a UnitSystem class, housing the unit system value itself, then $selection_data which is a PHP array of data read from database that has English units, and a $valueConverter which is the UnitConversionHelper in your example. Currently I mix them all together in the Controller and pass converted values to the view where it just displays them. What you are suggesting is to have two sets: of data CalculationResultInEnglishUnits CalculationResultInMetricUnits. Do I operate on them in the ... controller? – Dennis Jul 10 '18 at 20:47

It looks like I will be taking an approach of this:

Controller computes data, sends data as-is to the View, assuming English units.

View displays data using English, Metric, or both, doing conversions as needed. The library of conversion factors is supplied to the View, so View can do its own conversions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.