News:

Dear forum visitors, if the support forum is not available, please try again a few minutes later. Thanks!

Main Menu
Support-Forum

jDownloads 4 Router Warning “Undefined array key -1” + Incorrect SEF URLs When Using 4SEF, YOOtheme,

Started by ibrahim, 21.11.2025 10:50:13

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ibrahim

Fix for jDownloads Router Warning "Undefined array key -1" and Wrong SEF URLs Caused by YOOtheme + 4SEF Pagination

Hello everyone,

I want to share the complete fix for a routing problem I experienced with jDownloads 4.x when combined with YOOtheme Pro and 4SEF. This issue took a long time to identify and debug, so I hope this post helps anyone who faces the same problem.

PROBLEM SYMPTOMS

PHP warnings on category pages:

Warning: Undefined array key -1 in components/com_jdownloads/src/Helper/LegacyRouter.php on line XXX
Pagination URLs randomly changed format, for example:

/category?start=20

/some-category-alias/category?start=20

/some-category-alias/category/66-slug?start=20

Some category pages also lost their catid completely when navigating to page 2 or higher.

4SEF created multiple SEF URL groups for the same jDownloads category, depending on which Itemid YOOtheme produced in the pagination link.

ROOT CAUSES FOUND

jDownloads router assumed all URL segments were present.
The router used code like:

$segments[$count - 2]
When the URL was shortened (for example: /category?start=20), then:

$count = 1which means:
$segments[-1]
This caused the "Undefined array key -1" warnings on PHP 8.x.

4SEF stored multiple SEF URLs for the same category view.
One SEF entry was correct (based on the correct Itemid).
Another SEF entry was wrong (plain /category) coming from another Itemid.
Pagination sometimes used the wrong Itemid, so jDownloads received incomplete segments.

YOOtheme sometimes generated pagination links with reduced segments, which exposed the router's assumptions.

FIX APPLIED: SAFER ROUTING IN jDOWNLOADS

I updated the LegacyRouter to safely handle all segment counts, and to fall back to the active menu item when no catid is found. This completely eliminates the negative-index warnings.

Here is the corrected routing switch block:

switch ($segments[0])
{
case 'category':
if ($count > 1) {
$vars['view'] = 'category';
$vars['catid'] = (int) $segments[$count - 1];
} else {
$app = Factory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
$vars['view'] = 'category';

        if ($active && !empty($active->query)) {
            if (isset($active->query['catid'])) {
                $vars['catid'] = (int) $active->query['catid'];
            } elseif (isset($active->query['id'])) {
                $vars['catid'] = (int) $active->query['id'];
            }
        }
    }
    break;

case 'download':
    if ($count >= 3) {
        $vars['view']  = 'download';
        $vars['catid'] = (int) $segments[$count - 2];
        $vars['id']    = (int) $segments[$count - 1];
    } elseif ($count === 2) {
        $vars['view']  = 'download';
        $vars['id']    = (int) $segments[1];
    } else {
        $vars['view'] = 'download';
    }
    break;

case 'summary':
    $vars['view'] = 'summary';
    if ($count >= 3) {
        $vars['catid'] = (int) $segments[$count - 2];
        $vars['id']    = (int) $segments[$count - 1];
    } elseif ($count === 2) {
        $vars['id'] = (int) $segments[1];
    }
    break;


}

This makes the router fully safe against short URLs generated by SEF layers.

FIX APPLIED: CLEANUP OF INCORRECT 4SEF ENTRIES

In the database table #__forsef_urls, I found two groups of SEF entries for the same category:

The correct SEF path (based on the real category alias)

The incorrect SEF path: simply /category

The incorrect group belonged to a different Itemid and was being used by some pagination links. This caused URLs like:

/category?start=20

which then caused jDownloads to receive wrong segments.

To fix this, I removed or corrected the wrong SEF entries.

Example removal:

DELETE FROM #__forsef_urls
WHERE extension='com_jdownloads'
AND base_nonsef='index.php?option=com_jdownloads&Itemid=XXXX&view=category';

(Replace XXXX with the unwanted Itemid.)

After clearing 4SEF cache, pagination SEF URLs became consistent.

FINAL RESULT

After applying the safer routing in jDownloads and cleaning the incorrect SEF URLs in 4SEF:

No more "Undefined array key -1" warnings

Pagination works on all category pages

catid is always correctly resolved

SEF URLs remain consistent when navigating between pages

jDownloads routing is now stable even with shortened URLs generated by YOOtheme and 4SEF

WHY THIS ROUTING FIX IS BETTER

It prevents negative array offsets

It handles incomplete or reduced URL segments safely

It automatically restores catid using the active menu item

It improves compatibility with modern SEF systems

It makes jDownloads far more robust on PHP 8.x and with YOOtheme

If anyone needs the fully patched router file, I can provide it.
  •  

Arno

Hi ibrahim,
thank you very much for providing this information.

I haven't looked at it in detail yet, so I have a quick question:
Does this only apply to use in combination with 4SEF and YOOtheme?
Or do your changes also bring improvements if these extensions are not used?
Best Regards / Gruß
Arno
Please make a Donation for jDownloads and/or write a review on the Joomla! Extensions directory!
  •  

Arno

Hi Ibrahim,
looks good for me. So I have added your modification now to coming jDownloads 4.0.49 and 4.1.
Thanks!
 
Best Regards / Gruß
Arno
Please make a Donation for jDownloads and/or write a review on the Joomla! Extensions directory!
  •