0
votes

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
Why are you using the private keyword with a non-method function?MasterAM
What do you mean by "non-method function"? This function is used within componentParseRoute.ehz350
Precisely, it is not a class method. Start by removing that keyword. And enable PHP errors during development, that would have prevented this one. Next, what is your component's name? It should prefix the function names: function <Yourcomponentname>ParseRoute($segments) { etc.MasterAM

1 Answers

0
votes

@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.