【问题标题】:How to load one helper function in all the controller functions without calling it everytime in all the functions如何在所有控制器函数中加载一个辅助函数而不在所有函数中每次都调用它
【发布时间】:2017-09-11 09:53:32
【问题描述】:

我想在所有控制器函数中添加这些辅助函数

// this is my helper file custom_helper.php

// $autoload['helper'] = array('url','form', 'custom_helper');

function notifications()
{
    $CI = get_instance();
    $CI->load->model('custom_model');
    $select = "id, name";
    $tableName = "users";
    $whereCondition = "created_at = CURDATE()";
    $result = $CI->custom_model
                     ->FetchWithSelect($select,$tableName,$whereCondition);       
    return $result;
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    在 application/helper 文件夹中创建帮助文件:xyz_helper.php

    <?php
        defined('BASEPATH') OR exit('No direct script access allowed');
    
        function notifications() 
        {
            $CI = get_instance();
            $CI->load->model('custom_model');
            $select = "id, name";
            $tableName = "users";
            $whereCondition = "created_at = CURDATE()";
            $result = $CI->custom_model
                         ->FetchWithSelect($select,$tableName,$whereCondition);       
            return $result;
        }
    

    然后打开 application/config/autoload.php 文件:添加这个

    $autoload['helper'] = array('url', 'file','form','xyz');
    

    另外你不需要把“custom_helper”放在$autoload['helper']中,只需把“custom”和文件放在application/helper/custom_helper.php中

    【讨论】:

      【解决方案2】:

      在 /application/config/autoload.php 更改:

      $autoload['helper'] = array();
      

      $autoload['helper'] = array('html', 'url');
      

      【讨论】:

        【解决方案3】:

        您的控制器可能扩展了 Codeigniter 控制器类。所以,你想要的是另一个扩展它的类:

        <?php
        
        class Basecontroller extends CI_Controller
        {
            public function notifications()
            {
                $CI = get_instance();
                $CI->load->model('custom_model');
                $select = "id, name";
                $tableName = "users";
                $whereCondition = "created_at = CURDATE()";
                $result = $CI->custom_model
                             ->FetchWithSelect($select,$tableName,$whereCondition);       
                return $result;
            }
        }
        

        然后在你所有的控制器中,扩展那个类!

        <?php
        
        class Home extends Basecontroller
        {
            public function someActionOrOther()
            {
                $this->notifications(); // now works
            }
        }
        

        【讨论】:

        • 你的类命名在 CI 中是错误的,只有第一个字母在 CI 3 中应该是大写的> 这里解释 codeigniter.com/user_guide/general/styleguide.html#file-naming
        • 确实,我不是 CI 人,但我知道如何扩展一个类!
        • bt 如何在 CI 中执行这些操作
        • 你的控制器在扩展什么?
        • class Home 扩展 CI_Controller
        【解决方案4】:

        通过在每个控制器的构造函数中调用辅助函数,我认为不需要在每个控制器函数中调用辅助函数

        【讨论】:

          猜你喜欢
          • 2015-11-09
          • 2011-09-20
          • 1970-01-01
          • 2019-03-22
          • 1970-01-01
          • 2022-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多