尝试将 PHP 添加到 Joomla 网站时崩溃了

2022-01-06 00:00:00 php joomla

我以前从未做过任何 php 编码,但我使用的是来自 chronoforums 的论坛扩展,并且我正在尝试向论坛添加一些 php,以便如果用户登录,它将显示欢迎:$user如果没有,它将显示一个链接登录/注册.这是我试过的代码,但它使论坛页面崩溃了.

guest;如果($状态== 1){$url = "/论坛/登录";echo "<a href="$url">登录/注册</a>";} 别的 {echo "<p>欢迎:{$user->用户名}</p>";}?>

错误:

 Fatal error: Class 'GCoreExtensionsChronoforumsHelpersJFactory' not found in D:wampwwwadministratorcomponentscom_chronoforumsextensionschronoforumshelperselements.php on line 72调用栈# 时间记忆功能位置1 0.0008 687992 {main}() ..index.php:02 0.1207 9311448 JSite->dispatch() ..index.php:523 0.1248 9382296 JComponentHelper::renderComponent() ..application.php:2204 0.1318 9428368 JComponentHelper::executeComponent() ..helper.php:3355 0.1322 9495248 require_once('D:wampwwwcomponentscom_chronoforumschronoforums.php') ..helper.php:3556 0.1401 10315576 GCoreLibsAppJ::call() ..chronoforums.php:207 0.1402 10318272 GCoreLibsAppJ->dispatch() ..app_j.php:2908 0.2263 14214704 GCoreLibsView->renderView()..app_j.php:2089 0.2267 14328112 包括('D:wampwwwadministratorcomponentscom_chronoforumsextensionschronoforumsviewsindex.php')..view.php:6910 0.2268 14328112 GCoreExtensionsChronoforumsHelpersElements->header() ..index.php:6

这是我添加代码的整个页面:

<div class="cfu-navbar"><div class="cfu-inner"><span class="cfu-corners-top"><span></span></span><ul class="cfu-linklist"><li class="cfu-icon-home"><a accesskey="h" href="<?php echo r_("index.php?option=com_chronoforums"); ?>"><;?php echo l_('CHRONOFORUMS_BOARD_INDEX');?></a></li><?php if(GCoreLibsAuthorize::authorized('GCoreExtensionsChronoforumsChronoforums', 'make_posts')): ?><?php if(isset($subscribed)): ?><?php if(!empty($subscribed['Subscribed'])): ?><li class="cfu-icon-unsubscribe"><a title="<?php echo l_('CHRONOFORUMS_UNSUBSCRIBE_TOPIC'); ?>"href="<?php echo r_("index.php?option=com_chronoforums&cont=topics&act=unsubscribe&t=".$topic['Topic']['id']); ?>"><?php echo l_('CHRONOFORUMS_UNSUBSCRIBE_TOPIC');?></a></li><?php 其他:?><li class="cfu-icon-subscribe"><a title="<?php echo l_('CHRONOFORUMS_SUBSCRIBE_TOPIC_DESC'); ?>"href="<?php echo r_("index.php?option=com_chronoforums&cont=topics&act=subscribe&t=".$topic['Topic']['id']); ?>"><?php echo l_('CHRONOFORUMS_SUBSCRIBE_TOPIC');?></a></li><?php endif;?><?php endif;?><?php if(0): ?><li class="cfu-icon-bookmark"><a title="书签主题" href="">书签主题</a></li><?php endif;?><?php endif;?><span class="cfu-corners-bottom"><span></span></span></div>

<?php}函数头($data = array()){$forum = isset($data['forum']) ?$data['论坛'] : null;?><div class="cfu-navbar"><div class="cfu-inner"><span class="cfu-corners-top"><span></span></span><ul class="cfu-linklist cfu-leftside"><li class="cfu-icon-home"><a accesskey="h" href="<?php echo r_("index.php?option=com_chronoforums"); ?>"><?php echo l_('CHRONOFORUMS_BOARD_INDEX');?></a><?php if(!empty($forum)): ?><strong>‹</strong><a href="<?php echo r_("index.php?option=com_chronoforums&cont=forums&f=".$forum['id']); ?>"><?php echo$forum['title'];?></a><?php endif;?><div class="cfu-rightside"><form action="<?php echo r_('index.php?option=com_chronoforums&cont=search'); ?>"方法=帖子"名称=搜索表单"><字段集><input type="text" value="" size="20" id="cfu-search_keywords" name="skeywords" class="cfu-inputbox cfu-search cfu-tiny"/><input type="submit" name="forums_search" value="<?php echo l_('CHRONOFORUMS_SEARCH'); ?>"class="cfu-button2"/><input type="hidden" name="f" value=""/></fieldset></表单>

<span class="cfu-corners-bottom"><span></span></span></div>

<?php$user = JFactory::getUser();$status = $user->guest;如果($状态== 1){$url = "/论坛/登录";echo "<a href="$url">登录/注册</a>";}别的{echo "<p>欢迎:{$user->用户名}</p>";}?><?php}}

解决方案

当您在 GCoreExtensionsChronoforumsHelpers 中工作时,JFactory 类位于全局空间,你只需要像这样添加前缀,例如

$user = JFactory::getUser();

或者,您可以在顶部添加一个 use 语句,例如

命名空间 GCoreExtensionsChronoforumsHelpers;使用JFactory;

参见 http://php.net/manual/language.namespaces.global.php

I've never done any php coding before, but I'm using a forum extension from chronoforums and I'm trying to add some php to the forums so that if the user is logged in it will display Welcome: $user and if not it will display a link Login/Register. Here is the code I tried, but it crashed the forum page.

<?php 
  $user = JFactory::getUser();
  $status = $user->guest;
  if($status == 1) {
      $url = "/forum/login";
      echo "<a href="$url">Login/Register</a>";
  } else { 
      echo "<p>Welcome: {$user->username}</p>";
  }   
?>   

Error:

    Fatal error: Class 'GCoreExtensionsChronoforumsHelpersJFactory' not found in D:wampwwwadministratorcomponentscom_chronoforumsextensionschronoforumshelperselements.php on line 72
Call Stack
#   Time    Memory  Function    Location
1   0.0008  687992  {main}( )   ..index.php:0
2   0.1207  9311448 JSite->dispatch( )  ..index.php:52
3   0.1248  9382296 JComponentHelper::renderComponent( )    ..application.php:220
4   0.1318  9428368 JComponentHelper::executeComponent( )   ..helper.php:335
5   0.1322  9495248 require_once( 'D:wampwwwcomponentscom_chronoforumschronoforums.php' )  ..helper.php:355
6   0.1401  10315576    GCoreLibsAppJ::call( )    ..chronoforums.php:20
7   0.1402  10318272    GCoreLibsAppJ->dispatch( )    ..app_j.php:290
8   0.2263  14214704    GCoreLibsView->renderView( )  ..app_j.php:208
9   0.2267  14328112    include( 'D:wampwwwadministratorcomponentscom_chronoforumsextensionschronoforumsviewsindex.php' )  ..view.php:69
10  0.2268  14328112    GCoreExtensionsChronoforumsHelpersElements->header( )   ..index.php:6

Here is the whole page I added the code to:

<?php
/**
* ChronoCMS version 1.0
* Copyright (c) 2012 ChronoCMS.com, All rights reserved.
* Author: (ChronoCMS.com Team)
* license: Please read LICENSE.txt
* Visit http://www.ChronoCMS.com for regular updates and information.
**/
namespace GCoreExtensionsChronoforumsHelpers;
defined('_JEXEC') or die('Restricted access');
defined("GCORE_SITE") or die;
class Elements {
    function footer($data = array()){
        $topic = isset($data['topic']) ? $data['topic'] : null;
        $subscribed = isset($data['subscribed']) ? $data['subscribed'] : null;

        ?>
        <div class="cfu-navbar">
            <div class="cfu-inner">
            <span class="cfu-corners-top"><span></span></span>
            <ul class="cfu-linklist">
                <li class="cfu-icon-home"><a accesskey="h" href="<?php echo r_("index.php?option=com_chronoforums"); ?>"><?php echo l_('CHRONOFORUMS_BOARD_INDEX'); ?></a></li>

                <?php if(GCoreLibsAuthorize::authorized('GCoreExtensionsChronoforumsChronoforums', 'make_posts')): ?>
                    <?php if(isset($subscribed)): ?>
                        <?php if(!empty($subscribed['Subscribed'])): ?>
                        <li class="cfu-icon-unsubscribe"><a title="<?php echo l_('CHRONOFORUMS_UNSUBSCRIBE_TOPIC'); ?>" href="<?php echo r_("index.php?option=com_chronoforums&cont=topics&act=unsubscribe&t=".$topic['Topic']['id']); ?>"><?php echo l_('CHRONOFORUMS_UNSUBSCRIBE_TOPIC'); ?></a></li>
                        <?php else: ?>
                        <li class="cfu-icon-subscribe"><a title="<?php echo l_('CHRONOFORUMS_SUBSCRIBE_TOPIC_DESC'); ?>" href="<?php echo r_("index.php?option=com_chronoforums&cont=topics&act=subscribe&t=".$topic['Topic']['id']); ?>"><?php echo l_('CHRONOFORUMS_SUBSCRIBE_TOPIC'); ?></a></li>
                        <?php endif; ?>
                    <?php endif; ?>
                    <?php if(0): ?>
                    <li class="cfu-icon-bookmark"><a title="Bookmark topic" href="">Bookmark topic</a></li>
                    <?php endif; ?>
                <?php endif; ?>
            </ul>
            <span class="cfu-corners-bottom"><span></span></span></div>
        </div>
        <?php
    }

    function header($data = array()){
        $forum = isset($data['forum']) ? $data['forum'] : null;
        ?>
        <div class="cfu-navbar">
            <div class="cfu-inner">
            <span class="cfu-corners-top"><span></span></span>
            <ul class="cfu-linklist cfu-leftside">
                <li class="cfu-icon-home">
                    <a accesskey="h" href="<?php echo r_("index.php?option=com_chronoforums"); ?>"><?php echo l_('CHRONOFORUMS_BOARD_INDEX'); ?></a>
                    <?php if(!empty($forum)): ?>
                    <strong>‹</strong>
                    <a href="<?php echo r_("index.php?option=com_chronoforums&cont=forums&f=".$forum['id']); ?>"><?php echo $forum['title']; ?></a>
                    <?php endif; ?>
                </li>

            </ul>

            <div class="cfu-rightside">
                <form action="<?php echo r_('index.php?option=com_chronoforums&cont=search'); ?>" method="post" name="searchform">
                <fieldset>
                    <input type="text" value="" size="20" id="cfu-search_keywords" name="skeywords" class="cfu-inputbox cfu-search cfu-tiny"/>
                    <input type="submit" name="forums_search" value="<?php echo l_('CHRONOFORUMS_SEARCH'); ?>" class="cfu-button2"/>
                    <input type="hidden" name="f" value=""/>
                </fieldset>
                </form>
            </div>

            <span class="cfu-corners-bottom"><span></span></span></div>
        </div>
<?php 
$user = JFactory::getUser();
$status = $user->guest;
if($status == 1){
$url = "/forum/login";
echo "<a href="$url">Login/Register</a>";
}
else
{ 
echo "<p>Welcome: {$user->username}</p>";
}   
?>   
        <?php
    }
}

解决方案

As you are working in GCoreExtensionsChronoforumsHelpers and the JFactory class is in the global space, you simply need to prefix it as such, eg

$user = JFactory::getUser();

Alternatively, you could add a use statement at the top, eg

namespace GCoreExtensionsChronoforumsHelpers;

use JFactory;

See http://php.net/manual/language.namespaces.global.php

相关文章