【问题标题】:Use a function in $this->set() with CakePHP 2.1在 CakePHP 2.1 中使用 $this->set() 中的函数
【发布时间】:2013-11-12 20:42:27
【问题描述】:

我只是想知道如何使用 CakePHP 中的$this->set() 方法来使用/定义我自己的函数?我想做这样的事情......

AppController.php

<?php
    function checkSetup() {
        if ($this->Auth->user('setup') == 'notcomplete') { return true; }
    }

    $this->set('isSetup', checkSetup());
?>

然后我就可以在我的视图文件中访问和调用它了:

<?php if ($isSetup): ?>
You haven't setup your profile yet!
<?php endif; ?>

我已经尝试过了,但它显然不起作用,因为我遇到了一个严重的致命错误。关于如何做到这一点的任何想法/建议?

【问题讨论】:

    标签: cakephp-2.1


    【解决方案1】:
    $this->set('isSetup', checkSetup());
    

    该行需要在某个函数内才能被调用。大概你希望它在你的应用控制器的 beforFilter 中 - 像这样:

    <?php
    
    App::uses('Controller', 'Controller');
    
    class AppController extends Controller {
    
        function beforeFilter() {
            $this->set('isSetup', checkSetup());
        }
    
        function checkSetup() {
            if ($this->Auth->user('setup') == 'notcomplete') { return true; }
        }
    
    }
    
    ?>
    

    【讨论】:

    • 感谢您的帮助,我没有意识到它必须在函数中。干杯伙伴;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多