简体   繁体   中英

Joomla 2.5 - Generate URL based on article id

I am trying to generate an URL for an article basead on Article ID.

After runing this query

SELECT a.sectionid,
CASE WHEN CHAR_LENGTH( a.alias )
THEN CONCAT_WS( ":", a.id, a.alias )
ELSE a.id
END AS slug,
CASE WHEN CHAR_LENGTH( cc.alias )
THEN CONCAT_WS( ":", cc.id, cc.alias )
ELSE cc.id
END AS catslug
FROM #__content AS a
INNER JOIN #__categories AS cc ON cc.id = a.catid
WHERE a.id = $articleID

I store my result in $data and generate the link this way:

$link = JRoute::_(ContentHelperRoute::getArticleRoute($data[0]->slug, $data[0]->catslug, $data[0]->sectionid));

The problem is that the generated link its not correct when my article doesn't belong to any menu.

I noticed that when my article is not associated with any menu, the API gets the active menu id and add to the generated link, the parameter "&Itemid=MyActiveMenuId" (this happens in the route.php file). But since the article doesn't exist in the active menu, the generated link will not work.

I know that if the API just ignores the "item menu id" instead of getting the "active menu id" it will work, but I can do this without change the Joomla code? Also, I want that the "item menu id" continue to be considered for the cases where an article actually belongs to a menu, so the generated URL will be SEF.

There is any way to solve this? Or every article must belong to a menu item?

My Joomla version is 2.5.13

If you're trying to generate a URL based on the article ID ( $articleID in your code), your query should look something like the following:

select id from #__menu where link='index.php?option=com_content&view=article&id=$articleID';

For this example we'll assume to you stored the result in $menuID , or that $hasMenuID===false . You'll then determine your output URL like so:

if($hasMenuID===false) $outURL = "index.php?option=com_content&id=$articleID";
else $outURL = "index.php?option=com_content&itemid=$menuID";

If you want nice SEF-friendly URLs you can then apply JRoute . Also, please don't forget to typecast your $articleID to an integer before your initial query. SQL safety and all.

Every article need not be linked to menu to make it work.

You would want to run the url through JRoute to get the full url.

echo $id =JRequest::getVar('id');
$url = JRoute::_('index.php?option=com_content&view=article&id='.$id);

You can also redirect a url to another url with .htaccess There are also other extensions out there, which can help you redirect to a url.

For the rest of the url you would need to know the component you are calling (added as the option like above, Content manager is always com_content) and the view (which for an article is article). You can see the different views available for com_content by checking the file structure under /components/com_content/views/. Besides Article, you should see articles, categories, and category as well as a few other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM