JRoute and links to menu items

259 views
Skip to first unread message

Daniel Dimitrov

unread,
Oct 1, 2015, 7:13:05 AM10/1/15
to Joomla! General Development
I'm working on a project and I'm driving myself crazy with JRoute. And I really hope that I don't understand something.
I've developed a component for a customer. Due to the design of his website, not all pages look the same. So each menu entry has a body class and is styled slightly different than the others. So in my component it is important that when people do something on page A, they get redirect to page B and not to any random page, because that random page might not look like as expected.

here is a link that I have
index.php?option=com_capartments&view=Apartments

In the joomla menu I have a menu item that is set to the above link. The menu item title is "Mieten" (english -> rent). The sef link to the page is /mieten

So when I'm on the home page my module has a link JRoute::_('index.php?option=com_capartments&view=Apartments');

Unfortunately joomla doesn't recognize this as a link to the Apartment menu (/mieten) instead it generates the random link that we know component/capartment/?view=Apartments

If I put the itemid that I want this link to point to JRoute::_('index.php?option=com_capartments&view=Apartments&Itemid=105'); then the generated url changes to:

/mieten?view=Apartments

That looks better I thought. So let me place a router.php file and remove the view. Here is my code:
function CapartmentsBuildRoute(&$query)
{
$segments = array();

if (isset($query['view']))
{
$segments[] = $query['view'];
unset($query['view']);
}

return $segments;
}


So now my url is /mieten/Apartments but this is still not what I want. The link through the menu to the page is just /mieten

Right now I changed my l redirect links to Jroute::_('index.php?Itemid=105') and this generates the link that I want. But I cannot leave this like that. If tomorrow they create a new page for whatever reason I'll have to go everywhere in my code and look for that Itemid? Any ideas what I'm doing wrong?

Hannes Papenberg

unread,
Oct 1, 2015, 7:18:07 AM10/1/15
to joomla-de...@googlegroups.com
Which is why you need to find the right Itemid in your router.php,
remove all query parameters that are also in the menu item query and
then you get the right URLs. And please use the class based routers and
not the function based ones.

Since I guess that you are from Germany: I did a presentation about some
of this during the last Joomladay in Hamburg.

Hannes

Am 01.10.2015 um 13:13 schrieb 'Daniel Dimitrov' via Joomla! General
Development:
> $segments =array(); if (isset($query['view']))
> {
> $segments[] =$query['view']; unset($query['view']); }
>
> return $segments; }
>
>
> So now my url is /mieten/Apartments but this is still not what I want.
> The link through the menu to the page is just /mieten
>
> Right now I changed my l redirect links to
> Jroute::_('index.php?Itemid=105') and this generates the link that I
> want. But I cannot leave this like that. If tomorrow they create a new
> page for whatever reason I'll have to go everywhere in my code and
> look for that Itemid? Any ideas what I'm doing wrong?
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
> To post to this group, send email to
> joomla-de...@googlegroups.com
> <mailto:joomla-de...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/joomla-dev-general.
> For more options, visit https://groups.google.com/d/optout.

Daniel Dimitrov

unread,
Oct 1, 2015, 7:40:07 AM10/1/15
to Joomla! General Development
I was actually at the presentation, but obviously I haven't understood the context :) (point taken about the class routers)

So just to make sure that I understand you properly.
You are suggesting that I try to figure out automatically what the itemid is and then remove all params that are in the menu item query?

Not sure that I understand what you mean here. the buildRoute function is passed a query parameter. If I dump the content I see only
["option"]=> string(15) "com_capartments" ["lang"]=> string(5) "de-DE"

What am I supposed to unset?

Hannes Papenberg

unread,
Oct 1, 2015, 8:23:05 AM10/1/15
to joomla-de...@googlegroups.com
In that case you don't need to unset something. But you need to set the
correct Itemid in that query parameter.

Hannes

Am 01.10.2015 um 13:40 schrieb 'Daniel Dimitrov' via Joomla! General
> > an email to joomla-dev-gene...@googlegroups.com
> <javascript:>
> > <mailto:joomla-dev-gene...@googlegroups.com
> <javascript:>>.
> > To post to this group, send email to
> > joomla-de...@googlegroups.com <javascript:>
> > <mailto:joomla-de...@googlegroups.com <javascript:>>.
> <http://groups.google.com/group/joomla-dev-general>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.

Daniel Dimitrov

unread,
Oct 1, 2015, 8:46:24 AM10/1/15
to Joomla! General Development
Okay, you managed to totally confuse me :)

If I pass a link to JRoute like this: 'index.php?option=com_capartments&view=Apartments&Itemid=105' I still don't have a correct link.

As you can see I have an Itemid in the link, so why is the router building an url like this: /mieten?view=Apartments

The link in the menu is exactly the same index.php?option=com_capartments&view=Apartments
>     > an email to joomla-dev-general+unsub...@googlegroups.com
>     <javascript:>
>     > <mailto:joomla-dev-general+unsub...@googlegroups.com
>     <javascript:>>.
>     > To post to this group, send email to
>     > joomla-de...@googlegroups.com <javascript:>
>     > <mailto:joomla-de...@googlegroups.com <javascript:>>.
>     > Visit this group at
>     http://groups.google.com/group/joomla-dev-general
>     <http://groups.google.com/group/joomla-dev-general>.
>     > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-general+unsub...@googlegroups.com
> <mailto:joomla-dev-general+unsub...@googlegroups.com>.
Auto Generated Inline Image 1

Hannes Papenberg

unread,
Oct 1, 2015, 9:46:28 AM10/1/15
to joomla-de...@googlegroups.com
Handing in the Itemid is wrong. As you say yourself, that can change at
any time. You have to hand in the URL without the Itemid and then
calculate the correct Itemid by comparing the menu items for your
component with the query that your router got and choosing the right
one. Then you need to remove all query parameters that are already
encoded in your URL either via $segments or via the query of the menu
item. You are currently for example not removing the view parameter.
Joomla does NOT calculate the Itemid for you, but will only fallback on
the current active Itemid or the home Itemid. So calculating the right
one is the job of the component developer. Joomla also does not compare
the query with the given menu item and/or removes anything from it
(except the option parameter). So that, too, is your job. Since you hand
in a URL with the parameters "view" = "Appartments", Joomla appends that
to the URL that it gets from the menu item and that results in what you
got right now.

Hannes

Am 01.10.2015 um 14:46 schrieb 'Daniel Dimitrov' via Joomla! General
Development:
> Okay, you managed to totally confuse me :)
>
> If I pass a link to JRoute like this:
> 'index.php?option=com_capartments&view=Apartments&Itemid=105' I still
> don't have a correct link.
>
> As you can see I have an Itemid in the link, so why is the router
> building an url like this: /mieten?view=Apartments
>
> The link in the menu is exactly the same
> index.php?option=com_capartments&view=Apartments
>
>
>
> joomla-dev-gene...@googlegroups.com <javascript:>
> > <javascript:>
> > > <mailto:joomla-dev-gene...@googlegroups.com
> <javascript:>
> > <javascript:>>.
> > > To post to this group, send email to
> > > joomla-de...@googlegroups.com <javascript:>
> > > <mailto:joomla-de...@googlegroups.com <javascript:>>.
> > > Visit this group at
> > http://groups.google.com/group/joomla-dev-general
> <http://groups.google.com/group/joomla-dev-general>
> > <http://groups.google.com/group/joomla-dev-general
> <http://groups.google.com/group/joomla-dev-general>>.
> > > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>
> > <https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>>.
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Joomla! General Development" group.
> > To unsubscribe from this group and stop receiving emails from
> it, send
> > an email to joomla-dev-gene...@googlegroups.com
> <javascript:>
> > <mailto:joomla-dev-gene...@googlegroups.com
> <javascript:>>.
> > To post to this group, send email to
> > joomla-de...@googlegroups.com <javascript:>
> > <mailto:joomla-de...@googlegroups.com <javascript:>>.
> > Visit this group at
> http://groups.google.com/group/joomla-dev-general
> <http://groups.google.com/group/joomla-dev-general>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.

Daniel Dimitrov

unread,
Oct 1, 2015, 10:03:27 AM10/1/15
to Joomla! General Development
I provided the Itemid only to illustrate what's going on.

Com_content has a helperRouter that generates the correct url and ads the itemid to it. So I thought -> since the correct url for me is index.php?option=com_capartments&view=Apartments
let me hang the Itemid and see what will happen. And even if I go though all the trouble to get the correct Itemid I still won't have a correct url.

Why are you saying that I'm not removing the view from the query. I have this code there:
if (isset($query['view']))
{

$segments[] = $query['view'];
unset($query['view']);
}

Oh no, you are right. My brain must have frozen. I'm unsettling the query, but I'm adding the view to the $segments, so it will be in the url.

Now it all makes sense again. Thank you Hannes!
>     joomla-dev-general+unsub...@googlegroups.com <javascript:>
>     >     <javascript:>
>     >     > <mailto:joomla-dev-general+unsub...@googlegroups.com
>     <javascript:>
>     >     <javascript:>>.
>     >     > To post to this group, send email to
>     >     > joomla-de...@googlegroups.com <javascript:>
>     >     > <mailto:joomla-de...@googlegroups.com <javascript:>>.
>     >     > Visit this group at
>     > http://groups.google.com/group/joomla-dev-general
>     <http://groups.google.com/group/joomla-dev-general>
>     >     <http://groups.google.com/group/joomla-dev-general
>     <http://groups.google.com/group/joomla-dev-general>>.
>     >     > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>
>     >     <https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>>.
>     >
>     > --
>     > You received this message because you are subscribed to the Google
>     > Groups "Joomla! General Development" group.
>     > To unsubscribe from this group and stop receiving emails from
>     it, send
>     > an email to joomla-dev-general+unsub...@googlegroups.com
>     <javascript:>
>     > <mailto:joomla-dev-general+unsub...@googlegroups.com
>     <javascript:>>.
>     > To post to this group, send email to
>     > joomla-de...@googlegroups.com <javascript:>
>     > <mailto:joomla-de...@googlegroups.com <javascript:>>.
>     > Visit this group at
>     http://groups.google.com/group/joomla-dev-general
>     <http://groups.google.com/group/joomla-dev-general>.
>     > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send

Hannes Papenberg

unread,
Oct 1, 2015, 10:24:02 AM10/1/15
to joomla-de...@googlegroups.com
The *HelperRoute classes in Joomla are the eternal sin of routing and
I've been working hard to get us to remove that stupid idea for the last
6 years. They are the absolutely wrong approach to solve the Itemid
issue. As I said, that code has to be in the router.php file of your
component, not in some unknown class somewhere in the system.

Hannes

Am 01.10.2015 um 16:03 schrieb 'Daniel Dimitrov' via Joomla! General
Development:
> I provided the Itemid only to illustrate what's going on.
>
> Com_content has a helperRouter that generates the correct url and ads
> the itemid to it. So I thought -> since the correct url for me is
> index.php?option=com_capartments&view=Apartments
> let me hang the Itemid and see what will happen. And even if I go
> though all the trouble to get the correct Itemid I still won't have a
> correct url.
>
> Why are you saying that I'm not removing the view from the query. I
> have this code there:
> if (isset($query['view']))
> {
> > joomla-dev-gene...@googlegroups.com <javascript:>
> <javascript:>
> > > <javascript:>
> > > >
> <mailto:joomla-dev-gene...@googlegroups.com <javascript:>
> joomla-dev-gene...@googlegroups.com <javascript:>
> > <javascript:>
> > > <mailto:joomla-dev-gene...@googlegroups.com
> <javascript:>
> > <javascript:>>.
> > > To post to this group, send email to
> > > joomla-de...@googlegroups.com <javascript:>
> > > <mailto:joomla-de...@googlegroups.com <javascript:>>.
> > > Visit this group at
> > http://groups.google.com/group/joomla-dev-general
> <http://groups.google.com/group/joomla-dev-general>
> > <http://groups.google.com/group/joomla-dev-general
> <http://groups.google.com/group/joomla-dev-general>>.
> > > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>
> > <https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>>.
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Joomla! General Development" group.
> > To unsubscribe from this group and stop receiving emails from
> it, send
> > an email to joomla-dev-gene...@googlegroups.com
> <javascript:>
> > <mailto:joomla-dev-gene...@googlegroups.com
> <javascript:>>.
> > To post to this group, send email to
> > joomla-de...@googlegroups.com <javascript:>
> > <mailto:joomla-de...@googlegroups.com <javascript:>>.
> > Visit this group at
> http://groups.google.com/group/joomla-dev-general
> <http://groups.google.com/group/joomla-dev-general>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joomla-dev-gene...@googlegroups.com
> <mailto:joomla-dev-gene...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages