【问题标题】:Codeigniter auto autoload modelsCodeigniter 自动加载模型
【发布时间】:2016-05-16 17:10:39
【问题描述】:

我不想编写一个基于文件夹模型中的文件自动加载 :) 模型的函数。所以应用程序必须扫描文件夹中的文件,grep 所有 .php 文件,删除 .和..“文件夹”并将它们放在autoload['model'] = array

这是我当前在 autoload.php 文件中的代码

$dir    = './application/models';
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
$mods = '';
foreach ($files as $f){
    if(glob('*.php') ){
        $mods .= str_replace('.php','',"'".$f."',");
    }
}
$autoload['model'] = $mods;

而且我不断收到类似的错误

An uncaught Exception was encountered

Type: RuntimeException

Message: Unable to locate the model you have specified: 'admins','categories','companies','countries'
Filename: D:\wamp64\www\myapp\public_html\rest\system\core\Loader.php

Line Number: 344

看起来问题是当我将数组传递给 $autoload 变量时,它会威胁整个数组作为一个模型。你们能帮我解决我的问题吗?

【问题讨论】:

  • 如果你检查整个APPPATH . 'config/autoload.php' 文件,你会断定它已经被写入。也检查docs 中的this page。我的建议是完整阅读文档,至少一次。
  • 编辑,现在我看到您想将该函数放在第 135 行之前。

标签: php codeigniter


【解决方案1】:

我会选择类似的东西:


/application/config/autoload.php

autoload['model'] = array('autoload_models');

/application/models/Autoload_models_model.php

class Autoload_models_model extends CI_Model {

    public function __construct(){

        parent::__construct();

        // Scan directory where this (Autoload_models_model.php) file is located
        $model_files = scandir(__DIR__);

        foreach($model_files as $file){
            // Make sure we are not reloading autoload_models_model
            // Make sure we have a PHP file
            if(
               strtolower(explode('.', $file)[0]) !== strtolower(__CLASS__) &&
               strtolower(explode('.', $file)[1]) === 'php')
            {
                $this->load->model(strtolower($file));
            }
        }
    }
}

【讨论】:

  • 工作得很好,除了我不得不用$this->load->model(explode('.', $file)[0]);替换$this->load->model(strtolower($file));
【解决方案2】:

这是对我有用的解决方案。如果您发现任何更短或更好的代码,请告诉我

$dir    = './application/models';
$files = scandir($dir);
$models = array();
foreach ($files as $f){
    $file_parts = pathinfo($f);
    $file_parts['extension'];
    $correct_extension = Array('php');
    if(in_array($file_parts['extension'], $correct_extension)){
        array_push($models, str_replace('.php','',$f));
    }
}
$autoload['model'] = $models;

【讨论】:

    【解决方案3】:
    /* autoload model */
    function iteratorFileRegex( $dir, $regex )
    {
    
        $files = new FilesystemIterator( $dir );
        $files = new RegexIterator( $files, $regex );
    
        $models = array();
    
        foreach ( $files as $file )
        {
            $models[] = pathinfo( $file, PATHINFO_FILENAME ); // Post_Model
        }
    
        return $models;
    
    }
    
    $autoload['model'] = iteratorFileRegex( APPPATH . "models", "/^.*\.(php)$/" );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多