0

My understanding is that when building an MVC component for joomla we should keep the custom functions in the controller PHP file for that view. I was quickly building up a view on a component and have the following function working in the view to find the shortest distance between two points:

function distance($a, $b)
    {
list($lat1, $lon1) = $a;
list($lat2, $lon2) = $b;

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return $miles;
    }

I am then using other variable based information later in the view to call this function:

$distances = array_map(function($items) use($ref) {
$a = array_slice($items, 0);
return distance($a, $ref);
}, $items);
asort($distances);

When I moved the distance function into the controller file I am getting the following error when I try to load the view:

Fatal error: Call to undefined function distance() 

What should I change in the call or otherwise to properly format the function for the MVC architecture?

tereško
  • 58,060
  • 25
  • 98
  • 150
codacopia
  • 2,369
  • 9
  • 39
  • 58
  • Can you post the complete class files for the MVC path in question? – Brian Bolli Apr 17 '14 at 01:42
  • 1
    And the controller does not necessarily house all "custom" functions. Deciding where methods go in an MVC paradigm should be based on its purpose. For example, anything to do with data retrieval should go in the Model. Your distance method however might be better suited for a helper class as it appears to be a method you would utilize through out your code base. – Brian Bolli Apr 17 '14 at 01:49
  • I think @BrianBolli is right, it really sounds like a helper. Controllers should really be focused on actions. – Elin Apr 17 '14 at 03:07

0 Answers0