Adding router.php in Joomla component breaks site

692 views Asked by At

I'm trying to add routing for a custom component I made and I followed the docs on how to do this. But when I uploaded the router.php file to the component, it rendered the site blank. Not sure if I'm missing anything. It's as if the whole site is referring only this new file, thus giving me a blank page.

The component's URL with SEF URL on is:

index.php/en/products

And the way I built the router is to have links look like these:

index.php/en/products/trousers
index.php/en/products/555-123-20

component\router.php

function componentBuildRoute( &$query )
{
    $segments = array();

    if(isset($query['view'])) {
        $segments[] = $query['view'];
        unset($query['view']);
    }
    if(isset($query['cat'])) {
        $segments[] = $query['cat'];
        unset($query['cat']);
    }
    if(isset($query['itemid'])) {
        $segments[] = $query['itemid'];
        unset($query['itemid']);
    }
    if(isset($query['color'])) {
        $segments[] = $query['color'];
        unset($query['color']);
    }

    return $segments;
}

function componentParseRoute($segments) {
    $vars = array();
    switch($segments[0])
    {
        case 'listing':
               $vars['view'] = $this->chooseView($segments[1]);
               break;
        case 'item':
               $vars['id'] = $segments[1].'-'.$segments[2];
               break;
    }
    return $vars;
}

private function chooseView($cat) {
    switch($cat) {
        case '1':
            $cat = 'trousers';
            break;
        case '2':
            $cat = 'jackets';
            break;
    }
    return $cat;
}
1

There are 1 answers

0
ehz350 On BEST ANSWER

@MasterAM is correct about the private.

On a somewhat related note, router.php does not like variables sitting outside of functions. I tried to add a global array but it was not accessible. Instead I made a function to return the array.