0

Below is the have written the code router file in the component

namespace Test\Component\Test\Site\Service;

// No direct access
defined('_JEXEC') or die;

use Joomla\CMS\Component\Router\RouterViewConfiguration;
use Joomla\CMS\Component\Router\RouterView;
use Joomla\CMS\Component\Router\Rules\StandardRules;
use Joomla\CMS\Component\Router\Rules\NomenuRules;
use Joomla\CMS\Component\Router\Rules\MenuRules;
use Joomla\CMS\Factory;
use Joomla\CMS\Categories\Categories;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Categories\CategoryFactoryInterface;
use Joomla\CMS\Categories\CategoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\CMS\Menu\AbstractMenu;

/**
 * Class TestRouter
 *
 */
class Router extends RouterView
{
    private $noIDs;
    /**
     * The category factory
     *
     * @var    CategoryFactoryInterface
     *
     * @since  1.0.0
     */
    private $categoryFactory;

    /**
     * The category cache
     *
     * @var    array
     *
     * @since  1.0.0
     */
    private $categoryCache = [];

    public function __construct(SiteApplication $app, AbstractMenu $menu, CategoryFactoryInterface $categoryFactory, DatabaseInterface $db)
    {
        $params = Factory::getApplication()->getParams('com_test');
        $this->noIDs = (bool) $params->get('sef_ids');
        $this->categoryFactory = $categoryFactory;
        
        
            $itemsdetail = new RouterViewConfiguration('itemsdetail');
        $itemsdetail->setKey('catid')->setNestable();
            $this->registerView($itemsdetail);
            $ccItemdetail = new RouterViewConfiguration('itemdetail');
            $ccItemdetail->setKey('id')->setParent($itemsdetail, 'catid');
            $this->registerView($ccItemdetail);
            $itemdetailform = new RouterViewConfiguration('itemdetailform');
            $itemdetailform->setKey('id');
            $this->registerView($itemdetailform);

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

        $this->attachRule(new MenuRules($this));
        $this->attachRule(new StandardRules($this));
        $this->attachRule(new NomenuRules($this));
    }


    
            /**
             * Method to get the segment(s) for a category
             *
             * @param   string  $id     ID of the category to retrieve the segments for
             * @param   array   $query  The request that is built right now
             *
             * @return  array|string  The segments of this item
             */
            public function getItemsdetailSegment($id, $query)
            {
                $category = $this->getCategories(["access" => true])->get($id);

                if ($category)
                {
                    $path = array_reverse($category->getPath(), true);
                    $path[0] = '1:root';

                    if ($this->noIDs)
                    {
                        foreach ($path as &$segment)
                        {
                            list($id, $segment) = explode(':', $segment, 2);
                        }
                    }

                    return $path;
                }

                return array();
            }
        /**
         * Method to get the segment(s) for an itemdetail
         *
         * @param   string  $id     ID of the itemdetail to retrieve the segments for
         * @param   array   $query  The request that is built right now
         *
         * @return  array|string  The segments of this item
         */
        public function getItemdetailSegment($id, $query)
        {
            if (!strpos($id, ':'))
            {
                $db = Factory::getContainer()->get('DatabaseDriver');
                $dbquery = $db->getQuery(true);
                $dbquery->select($dbquery->qn('alias'))
                    ->from($dbquery->qn('#__test_item'))
                    ->where('id = ' . $dbquery->q($id));
                $db->setQuery($dbquery);

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

            if ($this->noIDs)
            {
                list($void, $segment) = explode(':', $id, 2);

                return array($void => $segment);
            }

            return array((int) $id => $id);
        }
            /**
             * Method to get the segment(s) for an itemdetailform
             *
             * @param   string  $id     ID of the itemdetailform to retrieve the segments for
             * @param   array   $query  The request that is built right now
             *
             * @return  array|string  The segments of this item
             */
            public function getItemdetailformSegment($id, $query)
            {
                return $this->getItemdetailSegment($id, $query);
            }

    
            /**
             * Method to get the id for a category
             *
             * @param   string  $segment  Segment to retrieve the ID for
             * @param   array   $query    The request that is parsed right now
             *
             * @return  mixed   The id of this item or false
             */
            public function getItemsdetailId($segment, $query)
            {
                if (isset($query['catid']))
                {
                    $category = $this->getCategories(["access" => true])->get($query['catid']);

                    if ($category)
                    {
                        foreach ($category->getChildren() as $child)
                        {
                            if ($this->noIDs)
                            {
                                if ($child->alias == $segment)
                                {
                                    return $child->id;
                                }
                            }
                            else
                            {
                                if ($child->id == (int) $segment)
                                {
                                    return $child->id;
                                }
                            }
                        }
                    }
                }

                return false;
            }
        /**
         * Method to get the segment(s) for an itemdetail
         *
         * @param   string  $segment  Segment of the itemdetail to retrieve the ID for
         * @param   array   $query    The request that is parsed right now
         *
         * @return  mixed   The id of this item or false
         */
        public function getItemdetailId($segment, $query)
        {
            if ($this->noIDs)
            {
                $db = Factory::getContainer()->get('DatabaseDriver');
                $dbquery = $db->getQuery(true);
                $dbquery->select($dbquery->qn('id'))
                    ->from($dbquery->qn('#__test_item'))
                    ->where('alias = ' . $dbquery->q($segment));
                $dbquery->where($dbquery->qn('category') . ' LIKE "%' . $query['catid'] . '%"');
                $db->setQuery($dbquery);

                return (int) $db->loadResult();
            }
            return (int) $segment;
        }
            /**
             * Method to get the segment(s) for an itemdetailform
             *
             * @param   string  $segment  Segment of the itemdetailform to retrieve the ID for
             * @param   array   $query    The request that is parsed right now
             *
             * @return  mixed   The id of this item or false
             */
            public function getItemdetailformId($segment, $query)
            {
                return $this->getItemdetailId($segment, $query);
            }

    /**
     * Method to get categories from cache
     *
     * @param   array  $options   The options for retrieving categories
     *
     * @return  CategoryInterface  The object containing categories
     *
     * @since   1.0.0
     */
    private function getCategories(array $options = []): CategoryInterface
    {
        $key = serialize($options);

        if (!isset($this->categoryCache[$key]))
        {
            $this->categoryCache[$key] = $this->categoryFactory->createCategory($options);
        }

        return $this->categoryCache[$key];
    }
}

and Route URL is Route::_('index.php?option=com_test&view=itemdetail&id='.(int) $item->id . '&catid=' . (int) $item->category);

I am not getting what is wrong but URL is getting redirected to the home page can someone tell me what can be the issue here this is for the Joomla4

1
  • 1
    What you'll get is dependent upon what menuitems associated with your com_test component there are. Could you update your question with details of what com_test menuitems are configured? The menuitems can be hidden, but they do have to be Published. For each, can you let us know what you have as the Menu Item Type, and what you need to select (if anything) in the field just below it. Usually with a front-end form you don't specify an id when you set up the menuitem, so you could try commenting out the line $itemdetailform->setKey('id'); and see if that works. Jul 14, 2023 at 15:53

1 Answer 1

1

There was no issue in my router file but it was in the

/components/com_componantname/src/Service/Category.php

previously in my file, I only added the


 * @copyright  2023 Super User
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Actors\Component\Actors\Site\Service;
// No direct access
defined('_JEXEC') or die;

use \Joomla\CMS\Categories\Categories;
/**
 * Content Component Category Tree
 *
 * @since  1.0.0
 */

To fix this issue added the below code in the above file and SEF worked for the Joomla4 Component

class Category extends Categories
{
    /**
     * Class constructor
     *
     * @param   array  $options  Array of options
     *
     * @since   11.1
     */
    public function __construct($options = array())
    {
        $options['table'] = '#__tablename';
        $options['extension'] = 'com_componentname';
        parent::__construct($options);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.