简体   繁体   English

使用CodeIgniter在辅助函数中获取全局变量

[英]Acquiring a global variable in a helper function with CodeIgniter

I'm making a function which will insert some simple HTML into my view. 我正在做一个函数,它将一些简单的HTML插入到我的视图中。 I've already done this several times without problems, but this time I'm going to use variables which are set in the Controller->extracted to the view. 我已经完成了几次没有问题,但是这次我将使用在Controller-> extracted the view中设置的变量。

Now, I obviously don't want to go away from my one-line shortcut: <?php echo showStatusMessages(); ?> 现在,我显然不想离开我的单行快捷方式: <?php echo showStatusMessages(); ?> <?php echo showStatusMessages(); ?> by checking if variables are set, then send them as parameters etc. as this just kills the point of the function call altogether. <?php echo showStatusMessages(); ?>通过检查是否设置了变量,然后将其作为参数等进行发送,因为这完全扼杀了函数调用的要点。

After some trying and failing I've ended up with a protected property which I'd like to use in my function. 经过一番尝试和失败之后,我得到了一个受保护的属性,该属性想在我的函数中使用。 Any idea how I can find a way around this? 知道如何找到解决方法吗?

// Outputs success/error messages
function showStatusMessages() {

    $variables = array(
        'success',
        'error'
    );

    foreach ($variables as $variable) {
        // Cannot access protected property CI_Loader::$_ci_cached_vars
        if ($variable = $GLOBALS['CI']->load->_ci_cached_vars[$variable]) ${$variable} = $variable;
        break;
    }

    ob_start();

    // Success message set
    if (isset($success)) :
        echo '<div class="message_box success_color">'.$success.'</div>';
    endif;

    // Error message set
    if (isset($error)) :
        echo '<div class="message_box error_color">'.$error.'</div>';
    endif;

    $msg = ob_get_contents();
    ob_end_clean();

    return $msg;

}

Is there a reason you aren't using a view? 您没有使用视图是有原因的吗?

<?php if (isset($success)) : ?>
    <div class="message_box success_color"><?php echo $success; ?></div>
<?php endif; ?>
<?php if (isset($error)) : ?>
    <div class="message_box error_color"><?php echo $error; ?></div>
<?php endif; ?>

And then calling this view using a one-liner, like this (assumes you named the view 'statusMessage': 然后像这样使用单线调用此视图(假设您将视图命名为“ statusMessage”:

<?php $this->load->view('statusMessage'); ?>

Doing it this way accomplishes what you're function is trying to; 通过这种方式可以完成您要执行的功能; you can call it multiple times from a single line. 您可以在一行中多次调用它。

Agree that it's not the preferred method however assigning a $GLOBAL will work just fine, and be accessible below the function runs in hierarchy. 同意这不是首选方法,但是分配$GLOBAL会很好,并且可以在层次结构下运行的函数下面访问。 You will run into troubles attempting to assign it to the $GLOBALS['ci'] . 尝试将其分配给$GLOBALS['ci']会遇到麻烦。

In your helper: 在您的助手中:

function saveSomething($id){
    $GLOBAL['savedVars'][] = $id;
}

And in your view after your helper has been called: 在您看来,在您的助手被调用之后:

var_dump($GLOBAL['savedVars']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM