Skip to main content
1 of 4
Tom
  • 43
  • 4

use router.php in simple component

I am having a simple component, basically a list view and a single view. I want to nice URLs so I copy pasted and modified the router.php from com_content like this:

class BookstoreRouter extends JComponentRouterView
{
    public function __construct($app = null, $menu = null)
    {
        $books = new JComponentRouterViewconfiguration('books');
        $books->setNestable();
        $this->registerView($books);

        $book = new JComponentRouterViewconfiguration('book');
        $book->setKey('id')->setParent($books);
        $this->registerView($book);

        parent::__construct($app, $menu);

        $this->attachRule(new JComponentRouterRulesMenu($this));
        $this->attachRule(new JComponentRouterRulesStandard($this));
        $this->attachRule(new JComponentRouterRulesNomenu($this));
    }

    public function getBookSegment($id, $query)
    {
        if (!strpos($id, ':'))
        {
            $db = JFactory::getDbo();
            $dbquery = $db->getQuery(true);
            $dbquery->select($dbquery->qn('alias'))
                ->from($dbquery->qn('#__bookstore_books'))
                ->where('id = ' . $dbquery->q($id));
            $db->setQuery($dbquery);

            $id .= ':' . $db->loadResult();
        }

        return array((int) $id => $id);
    }

    public function getBookId($segment, $query)
    {
       return (int) $segment;
    }
}

There seem to be a couple of problems with this and I can not figure out what it is. The links generated in the list view seems fine but I get these errors:

  1. Warning: array_reverse() expects parameter 1 to be array, boolean given in /var/www/public/libraries/src/Component/Router/Rules/StandardRules.php on line 279

I am not setting a key to the parent view but there is no key to use as far as I understand it. When setting some random key 'foo' the Warning disappears but that solution seems odd.

  1. When calling host\{booklist-menu-alias}\{item-title} the list view is displayed instead of the single view.

Any helpful advice is much appreciated, thanks.

Tom
  • 43
  • 4