Custom classes in CodeIgniter

前端 未结 6 572
走了就别回头了
走了就别回头了 2021-01-31 05:45

Seems like this is a very common problem for beginners with CodeIgniter, but none of the solutions I\'ve found so far seems very relevant to my problem. Like the topic says I\'m

6条回答
  •  一个人的身影
    2021-01-31 05:57

    After a brief google search, I was inspired to make my own autoloader class. It's a bit of a hack, since I use custom Codeigniter library to preform auto-loading, but for me this is the best way, that I'm aware of, of loading all the classes, I require, without compromising my application architecture philosophy, to fit it into Codeigniter way of doing things. Some might argue that Codeigniter is not the right framework for me and that might be true, but I'm trying things out and playing around with various frameworks and while working on CI, I came up with this solution. 1. Auto-load new custom library by editing applicaion/config/autoload.php to include:

    $autoload['libraries'] = array('my_loader');
    

    and any other libraries you might need. 2. Then add library class My_loader. This class will be loaded on every request and when its constructor is run, it will recursively search through all sub-folders and require_once all .php files inside application/service & application/models/dto folders. Warning: folders should not have dot in the name, otherwise function will fail

    findAllPhpFiles($path));
            }
    
            /**
             * Require all files
             */
            foreach ($toBeRequired as $class) {
                require_once $class;
            }
        }
    
        /**
         * Find all files in the folder
         * 
         * @param string $package
         * @return string[]
         */
        public function findAllPhpFiles($path)
        {
            $filesArray = array();
            // find everithing in the folder
            $all = scandir($path);
            // get all the folders
            $folders = array_filter($all, get_called_class() . '::_folderFilter');
            // get all the files
            $files = array_filter($all, get_called_class() . '::_limitationFilter');
    
            // assemble paths to the files
            foreach ($files as $file) {
                $filesArray[] = $path . '/' . $file;
            }
            // recursively go through all the sub-folders
            foreach ($folders as $folder) {
                $filesArray = array_merge($filesArray, $this->findAllPhpFiles($path . '/' . $folder));
            }
    
            return $filesArray;
        }
    
        /**
         * Callback function used to filter out array members containing unwanted text
         * 
         * @param string $string
         * @return boolean
         */
        protected static function _folderFilter($member) {
            $unwantedString = '.';
            return strpos($member, $unwantedString) === false;
        }
    
        /**
         * Callback function used to filter out array members not containing wanted text
         *
         * @param string $string
         * @return boolean
         */
        protected static function _limitationFilter($member) {
            $wantedString = '.php';
            return strpos($member, $wantedString) !== false;
        }
    }
    

提交回复
热议问题