(SEF) URLs : I can't rewrite URL for other views

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
RWLFR
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Apr 27, 2012 7:00 pm

(SEF) URLs : I can't rewrite URL for other views

Post by RWLFR » Sun Sep 25, 2022 5:59 pm

Hi !
I'm developing a Joomla 4 component, and I have issues to rewrite URL for the view Category.

In my site\src\Service\EventsNomenuRules.php I have this :

Code: Select all

	

	public function parse(&$segments, &$vars)
	{
    //with this url: http://localhost/j4x/my-events/event-n/event-title.html
		// segments: [[0] => event-n, [1] => event-title]
		// vars: [[option] => com_rwlevents, [view] => rwlevents, [id] => 0]

		$vars['view'] = 'event';
		$vars['id'] = substr($segments[0], strpos($segments[0], '-') + 1);
		array_shift($segments);
		array_shift($segments);
		return;
	}

		/**
	 * Build a menu-less URL
	 *
	 * @param   array  &$query     The vars that should be converted
	 * @param   array  &$segments  The URL segments to create
	 *
	 * @return  void
	 *
	 * @since   3.4
	 */
	public function build(&$query, &$segments)
	{
			if (!isset($query['view']) || (isset($query['view']) && $query['view'] !== 'event') || isset($query['format']))
		{
			return;
		}
		$segments[] = $query['view'] . '-' . $query['id'];
		// the last part of the url may be missing
		if (isset($query['alias'])) {
			$segments[] = $query['alias'];
			unset($query['alias']);
		}
		unset($query['view']);
		unset($query['id']);
	}
	
	
	

And then, in the \site\src\Service\Router.php, I have this :

Code: Select all

	public function __construct(SiteApplication $app, AbstractMenu $menu,
			CategoryFactoryInterface $categoryFactory, DatabaseInterface $db)
	{
		$this->categoryFactory = $categoryFactory;
		$this->db              = $db;

		$params = ComponentHelper::getParams('com_rwlevents');
		$this->noIDs = (bool) $params->get('sef_ids');

		$category = new RouterViewConfiguration('category');
		$category->setKey('id');
		$this->registerView($category);

		$events = new RouterViewConfiguration('events');
		$events->setKey('id');
		$this->registerView($events);

		$event = new RouterViewConfiguration('event');
		$event->setKey('id');
		$this->registerView($event);

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

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


The result is this one : for events, it works fine. The URL is rewritten like this :
rwl-events/event-249/sortie-du-single-do-what-u-like

But for Category, I have this result :
rwl-events?view=category&id=11&alias=1991

How can i do to make other views work too ?

Thanks !

Post Reply

Return to “Joomla! 4.x Coding”