2

I have 1 component with 2 views - default is view1 - secondary is view2.

When I associate it with the menu from the backend, Joomla creates the correct url with alias for SEO.

Example: www.sitename.com/view1

When from view1, I click the content and it goes to view2. To link it I use: ?option=com_myname&view=view2

The complete url will be: www.sitename.com/?option=com_myname&view=view2

Everything works, but I would like to make the url www.sitename.com/view2 and not www.sitename.com/?option=com_myname&view=view2

I wrote the router.php inside the component and register the name view.

$view1 = new JComponentRouterViewconfiguration('view1');
$view1 ->setKey('id');
$this->registerView($view1);

$view2 = new JComponentRouterViewconfiguration('view2');
$view2 ->setKey('id');
$this->registerView($view2);

I created a legacyrouter file inside the helper folder, but when inside function build I print the view highlight_string(print_r($item->query['view'] ,true));

it always gives me view1 even if in the url I wrote view2

I am not sure if the procedure is correct. In any case, how do I change the slug/alias?

There is no function like: RegisterViewAndAlias('?option=com_myname&view=view2', 'view2');

How do I change the slug?

2
  • Why don't you create a menu item for view2?
    – Sharky
    Aug 2 at 5:28
  • I need it work programmatically. The views can be infinite and so are the slugs. Aug 2 at 16:13

1 Answer 1

0

What you're trying to do is not usual in Joomla and is not recommended. Joomla simply doesn't work that way. The URLs are first defined by menu item structure and only then by individual component routers. Having control of the first path segment is normally only possible when your component is set as default (home) menu item. Other pages of your component can then resolve to this menu item and could have a single segment path.

That said, it is possible to achieve what you want by attaching a custom rule to the global router (an instance of Joomla\CMS\Router\SiteRouter, not your component router). In J3 this can be done by adding a parsepreprocess() method to your router. Below is a very basic, barely tested example:

public function parsepreprocess($router, $uri)
{
    if ($uri->getPath() === 'view2')
    {
        $uri->setPath('');
        $uri->setVar('option', 'com_example');
        $uri->setVar('view', 'view2');
    }
}

In J4 you'll need to attach the rule to the site router manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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