joomla 2.5错误页面

时间:2014-09-17 10:21:01

标签: php error-handling joomla2.5 menuitem

我为丢失的网址创建了一个错误页面(404页)。它是以标准方式完成的,或者是模板中的error.php文件。

错误页面工作正常,它正确地重定向到Joomla文章。我的问题是我想捕获用户点击的链接,然后生成404页面。我的想法是将一些代码放在error.php页面的顶部,该页面通过电子邮件向我发送了丢失的页面:

            <?php
            defined('_JEXEC') or die;
            $app = JFactory::getApplication(); 
            $menu = $app->getMenu();
            $menuItem = $menu->getItems( 'link', 'index.php?option=com_example&view=reporting', true );
            $ref = JRoute::_('index.php?Itemid='.$menuItem->id);
            $mailer = JFactory::getMailer();
            $config = JFactory::getConfig();
            $sender = array( 
                $config->getValue( 'config.mailfrom' ),
                $config->getValue( 'config.fromname' ) );

            $mailer->setSender($sender);
            $recipient = array( 'me@mysite.com' );

            $mailer->addRecipient($recipient);

            $body   = "Missing page is ".$ref;
            $mailer->setSubject('404 Error Generated by your site');
            $mailer->setBody($body);

            $send = $mailer->Send();
            if ( $send !== true ) {
                echo 'Error sending email: ' . $send->__toString();
            } else {
                echo 'Mail sent';
            }

            header("Location: http://www.domain.com/404-page.html"); /* Redirect browser */
            exit();?>

但我无法获得商品ID。我可以获得推荐页面$ _SESSION [&#34; origURL&#34;] = $ _SERVER [&#34; HTTP_REFERER&#34;];但这没用,因为它是我正在寻找的缺失页面的ID。

有什么想法吗?我想知道我是否需要创建一个会话变量,每次单击一个菜单项时都会更新该会话变量并访问该变量但它似乎有点过头了。

谢谢

艾伦

1 个答案:

答案 0 :(得分:0)

感谢David T,他指出了我正确的方向。我重新编码,它的工作!我已经在代码中添加了一些注释,希望它可以帮助其他人为Joomla 2.5构建错误页面(避免在3.0中尝试过它)

再次感谢David T

艾伦

            <?php
            defined('_JEXEC') or die;

            //Get reffering Id
            $referer = JURI::getInstance($_SERVER['HTTP_REFERER']);
            $ref = $referer->hasVar('Itemid')?$referer->getVar('Itemid'):JSite::getMenu()->getActive()->id;

            //Get the title and other menu details as needed form db

            $db = JFactory::getDbo();
            $query = $db->getQuery(true);
            $query = "SELECT * FROM `#__menu` WHERE id = ".$ref."";

            $db->setQuery( $query );
            $results = $db->loadObjectList();
            $holdingArray = array();

            if(count($results)) {
                foreach($results as $r) {
                    array_push ($holdingArray,$r->title,$r->menutype) ;
                    }
            }       

            // Set up mailer and get site name and other details
            $mailer = JFactory::getMailer();
            $config = JFactory::getConfig();
            $sender = array( 
            $config->getValue( 'config.mailfrom' ),
            $config->getValue( 'config.fromname' ) );
            $siteName = $config->getValue( 'config.sitename' );

            $mailer->setSender($sender);
            $recipient = array( 'youremail@yourdomain.com' );

            $mailer->addRecipient($recipient);

            $body   = "The menu item clicked was: ".$holdingArray[0]." which has id: ".$ref." which is in the the menu titled: ".$holdingArray[1];
            $body   .= "\n\rPlease note that sometimes the site isn't able to tell us accurately which menu item was clicked so this may prove to be wrong!";
            $mailer->setSubject('404 Error Generated by '.$siteName.' website');
            $mailer->setBody($body);

            //send mail
            $send = $mailer->Send();

            //Finally redirect to error page
            header("Location: http://www.yourdomain.com/404-page.html"); /* Redirect browser */
            exit();?>