New category: Joomla!
Since I use this CMS at work and also on my own pages, you can expect me to write about some problems I encountered during my work. Today's problem was getting a dynamic, correct URL no matter what SEF-settings are.
So here's the deal: I have some URLs on that page. SEF-Settings are partially on, so this means, the alias defines the URL. A colleague wanted to change the alias (and thus the URL), to optimize the page for search enginges. I could adjust all URLs manually each time we change something or I could make them as dynamic as possible.
First you need to figure out how to identify your fixed part: Is it the Article ID, the Menu ID or just the Name of the Menu? Maybe it's only the position the menu is in? For the latter two I can only give you hints, but you will surely figure out how to solve that problem with the indicators I give here.
Okay let's start: Joomla has a function called JRoute::_() which will return a url for a given string. Ths is the function we will be needing, but it won't work alone. Let's start with the article ID:
<?php
$_id = 42 // the article id
$url = JRoute::_(ContentHelperRoute::getArticleRoute($_id));
?>
That's pretty straightforward. Enter the ID (if you like take it all in one line). What getArticleRoute will yield is something like this:
index.php?option=com_content&view=article&id=42&Itemid=60
Notice the Itemid? Well here may lie your problem: Since you don't provide a menu, Joomla will choose an Itemid, but this will probably not contain your desired Itemid (which will be defined in the menu for example). If this already worked for you, you may quit here. But there is a way to get the correct Itemid. Well it's more like: You know the Itemid and will get everything else.
<?php
$_menuid = 75; //the ID of the Menu
$menu =& JSite::getMenu(); // menu object
$item =& $menu->getItem($_menuid); // item with id=$_menuid
$url = $item->link."&Itemid=$_menuid"; // get the url from $item; Note: we have to add the $_menuid as &Itemid to help JRoute get the correct link
echo JRoute::_($url); //build the URL according to the internal SEF-settings
?>
Define your itemid in $_menuid. To get it go to your Administration and look the for the Menu-Item you want to select. We will be generating the exact URL that this Menu is using, too. So the fixed part here will be our Menu-Item. If you change this, you will change the URL (or if you change where it points your new URL will point to this to, which is probably what we want). Well the Code above is comments and is pretty self-explanatory so I won't write anything more about it.
This is it. This will yield you the URL you need. In both cases we used JRoute::_() for our finale conversion, but the difference is what we put into this. If you do not provide and Itemid, JRoute::_() will produce a working URL. It's just pretty ugly:
/index.php/component/content/article/42.html
That's not really search enginge friendly, is it?
Now if your goal is to have a fixed position or menu name, you can use the $menu-object to find your desired item. Then get the menuID, and build the link in the same way (i.e. select your $item by your choice and use $item->link. Just don't forget to add your MenuID as Itemid.
One last note: I did not activate the second SEF-setting (removing the index.php/ from URL). So I do not know for sure this will still work, but the way it is build (using .htaccess) I think this will be no problem for the method. Please leave any feedback you have.
Probably I will add this information to the Joomla-Wiki if it's requested. But at least my forum post did not seem to generate any interest...