Skip to main content
2 of 4
added 547 characters in body
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.

Update

Problem seems to be my parent has no Id of some kind so I am getting this Warning. I can route to the menu item, which is my list view. From there I have a dilemma. I can register the books view, resulting in my totally correct item links will not work. Or I can have only the book view registered and not have the links build in my books view at all.

I did build a legacyrouter.php like the one com_content uses which works fine. But since it is marked as deprecate I really want to make it work with the JComponentRouterView.

Tom
  • 43
  • 4