Magento:创建自定义控制器 [英] Magento: create custom Controller

查看:60
本文介绍了Magento:创建自定义控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个新闻模块.一切正常.

http://domain.com/magento/news

此页面显示所有新闻项目.带有标题,内容和日期.

我想在用户单击查看更多链接"时使该内容的查看更多链接"将其重定向到特定的 newsitem 页面

http://domain.com/magento/news/newsitem-1

我用以下代码创建了另一个控制器newsitemController.php:

  public function infoAction(){

$this->loadLayout();
$this->getLayout()->getBlock('content')
     ->append($this->getLayout()->createBlock('news/newsitem')  );
$this->renderLayout();
}

还使用以下代码创建了块名称info.php:

public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getnewsitem()    
    { 
        if(!$this->hasData('news')) {
            $this->setData('news', Mage::registry('news'));
        }
        return $this->getData('news');
    }

未获得输出.

需要帮助才能获得输出.

解决方案

在您的 info.php 中添加以下函数以获取新闻条目的网址.

public function getItemUrl($newsItem)
{
    return $this->getUrl('*/*/view', array('id' => $newsItem->getId()));
}

在您的控制器中添加以下功能以查看新闻详细信息页面.

 public function viewAction()
  {
       $model = Mage::getModel('magentostudy_news/news');
       $model->load($newsId);
       $this->loadLayout();
       $itemBlock = $this->getLayout()->getBlock('news.info');
       $this->renderLayout();

  }

通过此操作,您可以简单地访问信息页面,并在此页面上附加此链接,以了解更多类似信息

 foreach ($this->getCollection() as $newsItem)
 {
 //Other Code
 <a href="<?php echo $this->getItemUrl($newsItem) ?>">Read More..</a>
 }

i have created one news module . it is working fine..

http://domain.com/magento/news

this page is displaying all the news items. with title, content and date.

i want to make view more link for content when user click on view more link it will redirect user to specific newsitem page

http://domain.com/magento/news/newsitem-1

i created another controller newsitemController.php with following code :

  public function infoAction(){

$this->loadLayout();
$this->getLayout()->getBlock('content')
     ->append($this->getLayout()->createBlock('news/newsitem')  );
$this->renderLayout();
}

also created block name info.php with below code:

public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getnewsitem()    
    { 
        if(!$this->hasData('news')) {
            $this->setData('news', Mage::registry('news'));
        }
        return $this->getData('news');
    }

not getting output..

need help to get output.

解决方案

In your info.php add the following function to get the url of the news item.

public function getItemUrl($newsItem)
{
    return $this->getUrl('*/*/view', array('id' => $newsItem->getId()));
}

In your controller add following function to view the news detail page.

 public function viewAction()
  {
       $model = Mage::getModel('magentostudy_news/news');
       $model->load($newsId);
       $this->loadLayout();
       $itemBlock = $this->getLayout()->getBlock('news.info');
       $this->renderLayout();

  }

By doing this you can simply access the info page attaching this link on read more like

 foreach ($this->getCollection() as $newsItem)
 {
 //Other Code
 <a href="<?php echo $this->getItemUrl($newsItem) ?>">Read More..</a>
 }

这篇关于Magento:创建自定义控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆