{"id":987,"date":"2019-08-21T16:06:41","date_gmt":"2019-08-21T08:06:41","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=987"},"modified":"2023-12-04T16:42:22","modified_gmt":"2023-12-04T08:42:22","slug":"codeigniter8-%e5%8a%a0%e8%bd%bd%e5%99%a8loader-php","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=987","title":{"rendered":"CodeIgniter(8)\u2014\u2014\u52a0\u8f7d\u5668Loader.php"},"content":{"rendered":"<p>Loader\u52a0\u8f7d\u5668\u662f\u57fa\u4e8eci\u6846\u67b6\u9879\u76ee\u5f00\u53d1\u4e2d\u7528\u7684\u6700\u5e7f\u6cdb\u7684\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u975e\u5e38\u65b9\u4fbf\u7684\u52a0\u8f7dmodel\uff0clibrary\uff0cconfig\u7b49\u7c7b\u548c\u6587\u4ef6\u3002 \u76f8\u4fe1\u719f\u6089\u7684CI\u7684\u670b\u53cb\u90fd\u77e5\u9053autoload\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u5427\uff0c\u53ea\u8981\u628a\u9700\u8981\u52a0\u8f7d\u7684model,libray,driver,helper,config\u7b49\u914d\u7f6e\u5230\u76f8\u5e94\u7684\u53c2\u6570\u4e2d\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u52a0\u8f7d\uff0c\u8fd9\u4e2a\u81ea\u52a8\u52a0\u8f7d\u7684\u5965\u79d8\u5c31\u5728Loader.php\u4e2d<\/p>\n<h3>1.\u81ea\u52a8\u52a0\u8f7d<\/h3>\n<p>\u81ea\u52a8\u52a0\u8f7d\u662f\u5728Loader\u7ec4\u4ef6\u52a0\u8f7d\u4e4b\u540e\u53d1\u751f\u7684\uff0c\u800cLoader\u7ec4\u4ef6\u52a0\u8f7d\u662f\u5728CI_Controller\u7684\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u5982\u4e0bCI_Controller\u6784\u9020\u51fd\u6570\u7684\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * Class constructor\n     *\n     * @return  void\n     *\/\n    public function __construct()\n    {\n        self::$instance =&amp; $this;\n\n        \/\/ Assign all the class objects that were instantiated by the\n        \/\/ bootstrap file (CodeIgniter.php) to local class variables\n        \/\/ so that CI can run as one big super object.\n        foreach (is_loaded() as $var =&gt; $class)\n        {\n            $this-&gt;$var =&amp; load_class($class);\n        }\n\n        $this-&gt;load =&amp; load_class('Loader', 'core');\n        $this-&gt;load-&gt;initialize();\n        log_message('info', 'Controller Class Initialized');\n    }\n<\/code><\/pre>\n<p>\u770b\u4e00\u4e0bLoader\u6e90\u7801\u5c31\u77e5\u9053$this->load->initialize();\u8fd9\u4e2a\u51fd\u6570\u53ea\u6709\u4e00\u53e5\u4ee3\u7801<\/p>\n<pre><code class=\"language-php line-numbers\"><br \/>    \/**\n     * Initializer\n     *\n     * @todo    Figure out a way to move this to the constructor\n     *      without breaking *package_path*() methods.\n     * @uses    CI_Loader::_ci_autoloader()\n     * @used-by CI_Controller::__construct()\n     * @return  void\n     *\/\n    public function initialize()\n    {\n        $this-&gt;_ci_autoloader();\n    }\n<\/code><\/pre>\n<p>\u7531\u6b64\u6211\u4eec\u53ef\u77e5\uff0c\u81ea\u52a8\u52a0\u8f7d\u662f\u5728\u63a7\u5236\u5668\u7684\u88ab\u521b\u5efa\u7684\u65f6\u5019\u53d1\u751f\u7684\uff0c\u90a3\u4e48\u81ea\u52a8\u52a0\u8f7d\u5177\u4f53\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5462\uff1f\u8fd9\u5c31\u8981\u770b_ci_autoloader\u51fd\u6570\u7684\u4ee3\u7801\u4e86\uff1a<\/p>\n<pre><code class=\"language-php line-numbers\">\/**\n     * CI Autoloader\n     *\n     * Loads component listed in the config\/autoload.php file.\n     *\n     * @used-by CI_Loader::initialize()\n     * @return  void\n     *\/\n    protected function _ci_autoloader()\n    {\n        if (file_exists(APPPATH.'config\/autoload.php'))\n        {\n            include(APPPATH.'config\/autoload.php');\n        }\n\n        if (file_exists(APPPATH.'config\/'.ENVIRONMENT.'\/autoload.php'))\n        {\n            include(APPPATH.'config\/'.ENVIRONMENT.'\/autoload.php');\n        }\n\n        if ( ! isset($autoload))\n        {\n            return;\n        }\n\n        \/\/ Autoload packages\n        if (isset($autoload['packages']))\n        {\n            foreach ($autoload['packages'] as $package_path)\n            {\n                $this-&gt;add_package_path($package_path);\n            }\n        }\n\n        \/\/ Load any custom config file\n        if (count($autoload['config']) &gt; 0)\n        {\n            foreach ($autoload['config'] as $val)\n            {\n                $this-&gt;config($val);\n            }\n        }\n\n        \/\/ Autoload helpers and languages\n        foreach (array('helper', 'language') as $type)\n        {\n            if (isset($autoload[$type]) &amp;&amp; count($autoload[$type]) &gt; 0)\n            {\n                $this-&gt;$type($autoload[$type]);\n            }\n        }\n\n        \/\/ Autoload drivers\n        if (isset($autoload['drivers']))\n        {\n            $this-&gt;driver($autoload['drivers']);\n        }\n\n        \/\/ Load libraries\n        if (isset($autoload['libraries']) &amp;&amp; count($autoload['libraries']) &gt; 0)\n        {\n            \/\/ Load the database driver.\n            if (in_array('database', $autoload['libraries']))\n            {\n                $this-&gt;database();\n                $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));\n            }\n\n            \/\/ Load all other libraries\n            $this-&gt;library($autoload['libraries']);\n        }\n\n        \/\/ Autoload models\n        if (isset($autoload['model']))\n        {\n            $this-&gt;model($autoload['model']);\n        }\n    }\n<\/code><\/pre>\n<h3>2.\u89c6\u56fe\u52a0\u8f7d<\/h3>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * View Loader\n     *\n     * Loads \"view\" files.\n     *\n     * @param   string  $view   View name\n     * @param   array   $vars   An associative array of data\n     *              to be extracted for use in the view\n     * @param   bool    $return Whether to return the view output\n     *              or leave it to the Output class\n     * @return  object|string\n     *\/\n    public function view($view, $vars = array(), $return = FALSE)\n    {\n        return $this-&gt;_ci_load(array('_ci_view' =&gt; $view, '_ci_vars' =&gt; $this-&gt;_ci_prepare_view_vars($vars), '_ci_return' =&gt; $return));\n    }\n\n<\/code><\/pre>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * Internal CI Data Loader\n     *\n     * Used to load views and files.\n     *\n     * Variables are prefixed with _ci_ to avoid symbol collision with\n     * variables made available to view files.\n     *\n     * @used-by CI_Loader::view()\n     * @used-by CI_Loader::file()\n     * @param   array   $_ci_data   Data to load\n     * @return  object\n     *\/\n    protected function _ci_load($_ci_data)\n    {\n        \/\/ Set the default data variables\n        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)\n        {\n            $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;\n        }\n\n        $file_exists = FALSE;\n\n        \/\/ Set the path to the requested file\n        if (is_string($_ci_path) &amp;&amp; $_ci_path !== '')\n        {\n            $_ci_x = explode('\/', $_ci_path);\n            $_ci_file = end($_ci_x);\n        }\n        else\n        {\n            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);\n            $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;\n\n            foreach ($this-&gt;_ci_view_paths as $_ci_view_file =&gt; $cascade)\n            {\n                if (file_exists($_ci_view_file.$_ci_file))\n                {\n                    $_ci_path = $_ci_view_file.$_ci_file;\n                    $file_exists = TRUE;\n                    break;\n                }\n\n                if ( ! $cascade)\n                {\n                    break;\n                }\n            }\n        }\n\n        if ( ! $file_exists &amp;&amp; ! file_exists($_ci_path))\n        {\n            show_error('Unable to load the requested file: '.$_ci_file);\n        }\n\n        \/\/ This allows anything loaded using $this-&gt;load (views, files, etc.)\n        \/\/ to become accessible from within the Controller and Model functions.\n        $_ci_CI =&amp; get_instance();\n        foreach (get_object_vars($_ci_CI) as $_ci_key =&gt; $_ci_var)\n        {\n            if ( ! isset($this-&gt;$_ci_key))\n            {\n                $this-&gt;$_ci_key =&amp; $_ci_CI-&gt;$_ci_key;\n            }\n        }\n\n        \/*\n         * Extract and cache variables\n         *\n         * You can either set variables using the dedicated $this-&gt;load-&gt;vars()\n         * function or via the second parameter of this function. We'll merge\n         * the two types and cache them so that views that are embedded within\n         * other views can have access to these variables.\n         *\/\n        empty($_ci_vars) OR $this-&gt;_ci_cached_vars = array_merge($this-&gt;_ci_cached_vars, $_ci_vars);\n        extract($this-&gt;_ci_cached_vars);\n\n        \/*\n         * Buffer the output\n         *\n         * We buffer the output for two reasons:\n         * 1. Speed. You get a significant speed boost.\n         * 2. So that the final rendered template can be post-processed by\n         *  the output class. Why do we need post processing? For one thing,\n         *  in order to show the elapsed page load time. Unless we can\n         *  intercept the content right before it's sent to the browser and\n         *  then stop the timer it won't be accurate.\n         *\/\n        ob_start();\n\n        \/\/ If the PHP installation does not support short tags we'll\n        \/\/ do a little string replacement, changing the short tags\n        \/\/ to standard PHP echo statements.\n        if ( ! is_php('5.4') &amp;&amp; ! ini_get('short_open_tag') &amp;&amp; config_item('rewrite_short_tags') === TRUE)\n        {\n            echo eval('?&gt;'.preg_replace('\/;*\\s*\\?&gt;\/', '; ?&gt;', str_replace('&lt;?=', '&lt;?php echo ', file_get_contents($_ci_path))));\n        }\n        else\n        {\n            include($_ci_path); \/\/ include() vs include_once() allows for multiple views with the same name\n        }\n\n        log_message('info', 'File loaded: '.$_ci_path);\n\n        \/\/ Return the file data if requested\n        if ($_ci_return === TRUE)\n        {\n            $buffer = ob_get_contents();\n            @ob_end_clean();\n            return $buffer;\n        }\n\n        \/*\n         * Flush the buffer... or buff the flusher?\n         *\n         * In order to permit views to be nested within\n         * other views, we need to flush the content back out whenever\n         * we are beyond the first level of output buffering so that\n         * it can be seen and included properly by the first included\n         * template and any subsequent ones. Oy!\n         *\/\n        if (ob_get_level() &gt; $this-&gt;_ci_ob_level + 1)\n        {\n            ob_end_flush();\n        }\n        else\n        {\n            $_ci_CI-&gt;output-&gt;append_output(ob_get_contents());\n            @ob_end_clean();\n        }\n\n        return $this;\n    }\n<\/code><\/pre>\n<p><strong>\u7531\u4ee5\u4e0a\u6e90\u7801\u53ef\u77e5\uff1a<\/strong><br \/>\n1. \u5982\u679c\u6211\u4eec\u5728\u81ea\u5df1\u7684\u4e1a\u52a1\u903b\u8f91\u4e2d\u6253\u5f00\u4e86\u7f13\u51b2\u533a\uff0c\u5e76\u4e14\u6ca1\u6709\u8fdb\u884c\u5173\u95ed\u7684\u8bdd\uff0c\u90a3\u4e48\u52a0\u8f7dview\u7684\u8f93\u51fa\u5185\u5bb9\u662f\u4e0d\u4f1a\u7ecf\u8fc7output\u7ec4\u4ef6\u7684\u3002<\/p>\n<ol start=\"2\">\n<li>\u8fd9\u6bb5\u4ee3\u7801\u5176\u5b9e\u6709\u4e00\u4e2abug\uff0c\u5982\u679c\u6211\u4eec\u5728\u4f20\u5165\u7684$_ci_vars\u53c2\u6570\u4e2d\u6709\u8fd9\u6837\u4e00\u4e2a\u53c2\u6570array(&#8216;_ci_path&#8217;=>&#8217;xxx.php&#8217;\uff0c\u56e0\u4e3a_ci_load\u51fd\u6570\u4f1a\u5c06_ci_vars\u6570\u7ec4\u7684\u5143\u7d20\u63d0\u53d6\u6210\u4e00\u4e2a$_ci_path=&#8217;xxx.php&#8217;\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u539f\u6765\u4fdd\u5b58\u7740\u52a0\u8f7d\u6587\u4ef6\u8def\u5f84\u7684$_ci_path\u53d8\u91cf\u5c31\u88ab\u8986\u76d6\u6389\u4e86\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u52a0\u8f7d\u5931\u8d25\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Loader\u52a0\u8f7d\u5668\u662f\u57fa\u4e8eci\u6846\u67b6\u9879\u76ee\u5f00\u53d1\u4e2d\u7528\u7684\u6700\u5e7f\u6cdb\u7684\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u5b83\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u975e\u5e38\u65b9\u4fbf\u7684\u52a0\u8f7dmodel\uff0clib [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[405],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-15","tag-php"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=987"}],"version-history":[{"count":1,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":988,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/987\/revisions\/988"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}