【问题标题】:redirecting from admin page to a postAction on frontend从管理页面重定向到前端的 postAction
【发布时间】:2012-09-04 14:32:07
【问题描述】:

编辑后的代码:现在我没有收到 404 错误,但另一方面,数据库或异常日志中没有添加任何内容。

我需要从我的模型 php 文件中调用第三方模块 (VideoTestimonials from aheadWorks) 上的 postAction 函数,以便重用他们的所有代码以获得所需的功能并避免将来出现错误,如果我只是复制代码并在会的。

previous question 上,我猜想,使用setRedirect 实现这一目标的正确方向。这是我在我的 php 模型文件中使用的代码,用于尝试重定向到前端的 postAction:

<?php
class Dts_Videotestimonials_Model_SearchVideo extends Mage_Core_Model_Abstract
{

    function printVideoEntry($videoEntry, $_product, $tabs = "")
    {
        # get user data
        $user = Mage::getSingleton('admin/session');
        $userName = $user->getUser()->getFirstname();
        $userEmail = $user->getUser()->getEmail();
        $data = array(
            "ProductId" => $_product->getId(),
                        "AuthorEmail" => $userEmail,
                        "AuthorName" => $userName,
                        "VideoLink" => $videoEntry->getVideoWatchPageUrl(),
                        "VideoType"  => "link",
                        "Title" => $videoEntry->getVideoTitle(),
                        "Comment" => "this is a comment"
        );
        $actionUrl = Mage::getUrl('vidtest/youtube/post', $data);
        Mage::app()->getResponse()->setRedirect($actionUrl);
    }
}

所有修改都可以到达这里,一步一步(在@Francesco的帮助下)我到了这一点。但是当我拨打这个电话时,我收到了404 Error。怎么了?

我正在研究这个SO question/answerthis post on the Magento forum,但我不能说清楚。第二个是来自事件观察者的重定向,我不知道它是否有帮助。

这是关于此错误的完整异常日志:

2012-09-04T14:25:17+00:00 ERR (3): 
exception 'Zend_Controller_Response_Exception' with message 'Invalid HTTP response code' in C:\wamp\www\magento\lib\Zend\Controller\Response\Abstract.php:286
Stack trace:
#0 C:\wamp\www\magento\lib\Zend\Controller\Response\Abstract.php(150): Zend_Controller_Response_Abstract->setHttpResponseCode(Array)
#1 C:\wamp\www\magento\app\code\core\Mage\Core\Controller\Response\Http.php(106): Zend_Controller_Response_Abstract->setRedirect('http://127.0.0....', Array)
#2 C:\wamp\www\magento\app\code\local\Dts\Videotestimonials\Model\SearchVideo.php(64): Mage_Core_Controller_Response_Http->setRedirect('http://127.0.0....', Array)
#3 C:\wamp\www\magento\app\code\local\Dts\Videotestimonials\Model\SearchVideo.php(198): Dts_Videotestimonials_Model_SearchVideo->printVideoEntry(Object(Zend_Gdata_YouTube_VideoEntry), Object(Mage_Catalog_Model_Product))
#4 C:\wamp\www\magento\app\code\local\Dts\Videotestimonials\Model\SearchVideo.php(244): Dts_Videotestimonials_Model_SearchVideo->printVideoFeed(Object(Zend_Gdata_YouTube_VideoFeed), Object(Mage_Catalog_Model_Product), 'Search results ...')
#5 C:\wamp\www\magento\app\code\local\Dts\Videotestimonials\controllers\Adminhtml\VideotestimonialsbackendController.php(28): Dts_Videotestimonials_Model_SearchVideo->searchAndPrint('s')
#6 C:\wamp\www\magento\app\code\core\Mage\Core\Controller\Varien\Action.php(419): Dts_Videotestimonials_Adminhtml_VideotestimonialsbackendController->postAction()
#7 C:\wamp\www\magento\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('post')
#8 C:\wamp\www\magento\app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#9 C:\wamp\www\magento\app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#10 C:\wamp\www\magento\app\Mage.php(683): Mage_Core_Model_App->run(Array)
#11 C:\wamp\www\magento\index.php(87): Mage::run('', 'store')
#12 {main}

【问题讨论】:

  • 看起来您正在重定向到一个不存在的页面。在这种情况下,url 不会映射到控制器,以便应用程序可以正确处理您的请求。如果打印出 Mage::getBaseUrl().'vidtest/youtube/post' 的内容,那么完整的 URL 是什么?如果您导航到该 URL,您是否会收到 404(未找到文件)或某些应用程序错误?
  • hmmm...尝试了echo 来查看$actionUrl,但它不正确:http://127.0.0.1/magento/index.php/http:/index/127.0.0.1/magento/index.php/vidtest/youtube/。似乎它搞砸了getbaseurl 和其他东西。我当然会得到404 Error

标签: php magento redirect magento-1.7


【解决方案1】:

你使用 getUrl 和 getBaseUrl 是一回事,你可以使用这段代码:

$this->_redirect('module/controller/action','parameters');

但是你在你的班级上扩展了 Mage_Adminhtml_Controller_Action。这是最好的方法。如果不是控制器,所以你不应该做这个重定向。

【讨论】:

  • 更正了代码,实际上我没有收到 404 错误。但现在我什么也没有得到,数据库或异常日志中没有插入。从模型文件中,我可以使用Mage::app()-&gt;getResponse()-&gt;setRedirect($actionUrl); 进行重定向。您的示例确实是从控制器操作中调用的。也许我需要扩展扩展的控制器操作并从模块中调用它?这也被建议作为我上一个问题的可能解决方案
  • +1 @Guerra 我没有注意到这段代码在模型中。您的数据存储应该在模型中完成,您需要的任何重定向或应用程序逻辑都应该在控制器中完成。
  • @renab 是对的 Yaroslav,您需要将控制器扩展至此类,并使用一个模型进行数据库持久化。这就是为什么 magento 是 MVC。
  • 谢谢两位,明天第一个小时回到我的办公桌前进行测试;)
猜你喜欢
  • 1970-01-01
  • 2011-12-25
  • 2014-02-22
  • 2017-07-23
  • 2012-09-04
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多