【问题标题】:How say to this joomla module to take me to my SEF url article?如何对这个 joomla 模块说带我到我的 SEF url 文章?
【发布时间】:2014-10-12 05:28:20
【问题描述】:

此模块从 joomla 中的类别中获取文章并像博客一样显示它们(短)。问题是当我点击标题时它给出了&view=item&layout=edit&sliderid=1&id=2。不友好,链接在我的主页中打开文章。如何让它产生正确的 SEF 链接,就像假设的那样?我使用 joomla 3 并启用了 mod_rewrite

代码如下:

defined('_JEXEC') or die('Restricted access');
    require_once(JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');

class PBHSourceJcontent {

public $arg;

public function loadFromDB($itemId = -1) {

    $myQuery = 'SELECT 
                i.* 
                FROM #__content as i 
                WHERE 1=1 
                AND i.state = 1';
    if ($itemId < 0 && !empty($this->arg['categories'])) {
        $myQuery .= ' AND i.catid IN (' .implode(',',$this->arg['categories']). ')';
    }
    if ($this->arg['ordering'] == 'random' && !empty($this->arg['loaded_items'])) {
        $myQuery .= ' AND i.id NOT IN ('.implode(',',$this->arg['loaded_items']).')';
    }
    if ($itemId < 0 && !empty($this->arg['ordering'])) {
        switch ($this->arg['ordering']) {
            case 'title_asc': 
                $myQuery .= ' ORDER BY i.title ASC';
                break;
            case 'title_desc': 
                $myQuery .= ' ORDER BY i.title DESC';
                break;
            case 'date_asc': 
                $myQuery .= ' ORDER BY i.publish_up ASC';
                break;
            case 'hits_desc': 
                $myQuery .= ' ORDER BY i.hits DESC';
                break;
            case 'hits_asc': 
                $myQuery .= ' ORDER BY i.hits ASC';
                break;
            case 'article_order': 
                $myQuery .= ' ORDER BY i.ordering ASC';
                break;
            case 'random':
                $myQuery .= ' ORDER BY RAND()';
                break;
            default: 
                $myQuery .= ' ORDER BY i.publish_up DESC';
                break;
        }
    }
    if ($itemId < 0 && !empty($this->arg['items_number'])) {
        $myQuery .= ' LIMIT ' . $this->arg['items_number'];
    }
    if ($itemId < 0 && !empty($this->arg['items_offset']) && $this->arg['items_offset'] > 0 && $this->arg['ordering'] != 'random') {
        $myQuery .= ' OFFSET ' . $this->arg['items_offset'];
    }
    if ($itemId < 0) {
        return DMHData::loadObjectList($myQuery);
    } else {
        $myQuery .= ' AND i.id = ' . $itemId;
        return DMHData::loadObject($myQuery);
    }
}

public function getItemId($item) {

    return $item->id;
}

public function getItemUrl($item) {

    if ($this->arg['previewpopup'] == 'enabled') {
        $myLink = 'href="#" onclick="DMPinboard.getPreview('.$item->id.');return false;"';
    } else {
        $link = 'index.php?option=com_content&view=article&id='.$item->id;
        $link = ContentHelperRoute::getArticleRoute($item->id, $item->catid);
        $myLink = 'href="'.urldecode($link).'"';
    }

    return $myLink;
}

public function getItemTitle($item) {

    if ($this->arg['show_title'] == 'yes') {
        return $item->title;
    } else {
        return '';
    }
}

public function getItemImage($item) {

    if ($this->arg['show_image'] == 'fromtext') {
        $myImage = '';
        $output = preg_match( '/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $item->introtext, $matches);
        if ($output > 0) {
            $myImage = $matches[1];
        }
        return $myImage;
    } else if($this->arg['show_image'] == 'introimg') {
        $images = json_decode($item->images);
        return $images->image_intro;
    } else if ($this->arg['show_image'] == 'fullimg') {
        $images = json_decode($item->images);
        return $images->image_fulltext;
    } else {
        return '';
    }
}

public function getItemIntro($item) {

    if ($this->arg['show_intro'] == 'yes') {
        $outText = str_replace(array("\t","\n","\r","\r\n"),'',strip_tags($item->introtext));
        if (!empty($this->arg['introlength']) && $this->arg['introlength'] > 0) {
            $outText = PBHHtml::shorter($outText, $this->arg['introlength']);
        }
        return $outText;
    } else {
        return '';
    }
}

public function getPreviewImage($item) {

    if ($this->arg['show_popup_images'] == 'fromtext') {
        $myImage = '';
        $output = preg_match('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $item->introtext, $matches);
        if ($output > 0) {
            $myImage = $matches[1];
        }
        return $myImage;
    } else if ($this->arg['show_popup_images'] == 'fullimg') {
        $images = json_decode($item->images);
        return $images->image_fulltext;
    } else {
        return '';
    }
}

public function getPreviewTitle($item) {

    if ($this->arg['show_popup_title'] == 'linked') {
        $link = 'index.php?option=com_content&view=article&id='.$item->id;
        $link = ContentHelperRoute::getArticleRoute($item->id, $item->catid);
        return '<a href="'.urldecode($link).'">'.$item->title.'</a>';
    } else if ($this->arg['show_popup_title'] == 'yes') {
        return $item->title;
    } else {
        return '';
    }
}

public function getPreviewContent($item) {

    if ($this->arg['show_popup_intro'] == 'withoutimg') {
        $outText = preg_replace('/<img[^>]+\>/i', '', $item->introtext);
    } else if($this->arg['show_popup_intro'] == 'yes') {
        $outText = preg_replace_callback('/(src=["\'])([^"\']+)(["\'])/','PBHHtml::checkImgSrc',$item->introtext);
    } else {
        $outText = '';
    }
    if (!empty($this->arg['previewlength']) && $this->arg['previewlength'] > 0) {
        $outText = PBHHtml::truncate_teaser($outText,$this->arg['previewlength']);
    }
    return $outText;
}

public function getPreviewReadmore($item) {

    if ($this->arg['show_popup_articlelink'] == 'yes') {
        $link = 'index.php?option=com_content&view=article&id='.$item->id;
        $link = ContentHelperRoute::getArticleRoute($item->id, $item->catid);
        return '<a href="'.urldecode($link).'">'.JText::_('COM_DMPINBOARD_FRONTEND_READMORE').'</a>';
    } else {
        return '';
    }
}

public function getShareInfo($item) {

    $share = array();
    //---
    $link = 'index.php?option=com_content&view=article&id='.$item->id;
    $link = ContentHelperRoute::getArticleRoute($item->id, $item->catid);
    $share['url'] = urldecode(JUri::base().$link);
    //---
    $share['title'] = $item->title;
    return $share;
}

}

?>

【问题讨论】:

  • “在我的主页打开文章”是什么意思?还请显示您在布局中的内容。您应该使用更多的 Joomla API 来执行此操作(例如您的查询),并解释为什么您在多个位置连续分配 $link 两行。我建议您只需查看包含文章链接的几个核心模块之一,然后从那里复制代码。
  • 感谢您的回答。首先我是 php 的一个小新手!好的,我的意思是这个。假设我去一个普通类别的博客并点击一篇文章,我希望我的浏览器能将我引导到我的文章存储位置的正确链接路径。 www.vgespies.com/stages/id-alias 所以我想在那里展示我的文章作为一个孩子的 SEF ulr 我不知道如何命令这个组件来生产 SEF url
  • 我以为你说你是在制作一个模块而不是一个组件。请澄清这一点。但是,无论您的最佳选择是查看核心如何创建 SEF url 并执行完全相同的操作。这真的很难知道,因为您只是转储了很多代码,而不是显示代码的相关部分。也请回答我问的问题,为什么你要分配 $link 两次,为什么你的查询是这样的,以及你在主页中打开的意思。 Joomla 有一个你需要使用的 API。

标签: php mod-rewrite joomla


【解决方案1】:

我假设您的意思是在 getItemUrl($item) 函数的第二部分输出一个 SEF url。

简单

$link = JRoute::_('index.php?option=com_content&view=article&id='.$item->id);

会的。但是在您的问题中,在您的代码上方,您显示 &amp;view=item&amp;layout=edit&amp;sliderid=1&amp;id=2 这不是下面代码中的 url,并且不会带您进入文章视图。

【讨论】:

    猜你喜欢
    • 2011-02-14
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    相关资源
    最近更新 更多