在组件的基本文件中(components/com_yourname/yourname.php
位于
$app = JFactory::getApplication();
$menu = $app->getMenu();
$jinput = $app->input;
// check if they are on the default menu item (i.e. not your component's)
// also check if the template is set already
if ($menu->getActive() == $menu->getDefault() && $jinput->get('template') != 'comtemplate') {
// you might be able to get away with this, I haven't tested this
$jinput->set('template', 'comtemplate');
// otherwise set up a redirect
// get the current url of the page
$url = $_SERVER['REQUEST_URI'];
// check if '?' already exists in the url
// if it does then there is already the start of the query string so we should
// use '&' to tack on the new query item
// if not then start the query string with '?'
$url .= (strpos($url, '?')===FALSE ? '?' : '&') . 'template=comtemplate';
// take the new url and redirect to it.
//This should update the url in the browser for the user
$app->redirect($url);
// since you redirected, no sense letting anything else run here, so exit
exit();
}
更新:我在代码中添加了更好的注释,所以希望你能看到我想要做什么。