Router foreach() line 109 or 106 error fix
Debugging and fixing the Joomla route.php 109 problem
The problem: http://internetconsultant.mkmarketer.co.uk/
Warning: Invalid argument supplied for foreach() in .../public_html/components/com_content/helpers/route.php on line 109
Warning: Invalid argument supplied for foreach() in .../public_html/components/com_content/helpers/route.php on line 109
Warning: Invalid argument supplied for foreach() in .../public_html/components/com_content/helpers/route.php on line 109
This problem comes about after the site was moved data between servers. The new server has different PHP warning settings so the problem shas displayed where previously the problem would have remained hidden or logged only in audit files.
The existing Joomla code:
function _findItem($needles) {
$component =& JComponentHelper::getComponent('com_content');
$menus = &JApplication::getMenu('site', array());
$items = $menus->getItems('componentid', $component->id);
$match = null;
foreach($needles as $needle => $id) {
foreach($items as $item) {
if ((@$item->query['view'] == $needle) && (@$item->query['id'] == $id)) {
$match = $item; break;
}
}
if(isset($match)) { break; } } return $match;
}
Line 109 is in and about the inner foreach loop so tracking one step back we have a reference to $menus which is a valid object else we would have an object reference error.
The $items is assumed to be an array of objects but something must have gone bad with a call to $menus->getItems('componentid', $component->id). I use multi-sites and have just ported between servers so the menu items may be detatched from the shared articles being referred to by Joomla Multi-Sites.
Did the obvious thing and ported to use the same table names but shared multi-site articles uses symbolic names and data stored in directory files. Three of the same errors and four multi-sites shared articles - gut says that's the place.
New symbolic name: xyzabc and pre-named table names xyzabc_
Old symbolic name: same as above - they match OK
OK, let's check the articles in the data files.
Well the article id I want is 69 and the section id set on it is 0.
Let's just check what I'm pointing at on the problem site:
http://internetconsultant.mkmarketer.co.uk/ is abcdefghi_ so I lookup the menu file.
As expected we have: index.php?option=com_multisitescontent&view=article&site_id=xyzabc&id=69
Well that ties up and the other multi-site shared articles... all as expected.
This site is dominated by shared multi-site articles so I unpublish them at menu level and test. Yes, problem goes away so I add a 'Contact Us' article and include it on the menu as a published single article. Now the Joomla system has one article of data which will turn into an array collection to be processed by the routine above.
I re-publish the multi-site articles and have no more errors or warnings.
Problem solved but a lesson to all developers:
"You never know how your code will be used so make it robust and able to withstand abuse."
