{"id":980,"date":"2019-08-16T09:46:51","date_gmt":"2019-08-16T01:46:51","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=980"},"modified":"2023-12-04T16:42:22","modified_gmt":"2023-12-04T08:42:22","slug":"codeigniter5-%e9%92%a9%e5%ad%90hook-php","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=980","title":{"rendered":"CodeIgniter(5)\u2014\u2014\u94a9\u5b50Hook.php"},"content":{"rendered":"<h3>1.\u94a9\u5b50\u662f\u4ec0\u4e48\uff1f<\/h3>\n<p>\u94a9\u5b50\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5904\u7406\u6d88\u606f\u7684\u7a0b\u5e8f\u6bb5\uff0c\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\uff0c\u628a\u5b83\u6302\u5165\u7cfb\u7edf\u3002\u6bcf\u5f53\u7279\u5b9a\u7684\u6d88\u606f\u53d1\u51fa\uff0c\u5728\u6ca1\u6709\u5230\u8fbe\u76ee\u7684\u7a97\u53e3\u524d\uff0c\u94a9\u5b50\u7a0b\u5e8f\u5c31\u5148\u6355\u83b7\u8be5\u6d88\u606f\uff0c\u4ea6\u5373\u94a9\u5b50\u51fd\u6570\u5148\u5f97\u5230\u63a7\u5236\u6743\u3002\u8fd9\u65f6\u94a9\u5b50\u51fd\u6570\u5373\u53ef\u4ee5\u52a0\u5de5\u5904\u7406\uff08\u6539\u53d8\uff09\u8be5\u6d88\u606f\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u4f5c\u5904\u7406\u800c\u7ee7\u7eed\u4f20\u9012\u8be5\u6d88\u606f\uff0c\u8fd8\u53ef\u4ee5\u5f3a\u5236\u7ed3\u675f\u6d88\u606f\u7684\u4f20\u9012\u3002<\/p>\n<h3>2.CI\u4e2d\u94a9\u5b50\u7684\u4f7f\u7528<\/h3>\n<p>\u94a9\u5b50\u662f\u4e8b\u4ef6\u9a71\u52a8\u7684\uff0c\u5305\u542b\u4e00\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f<\/li>\n<li>\u6ce8\u518c\u4e8b\u4ef6<\/li>\n<li>\u89e6\u53d1\u4e8b\u4ef6<\/li>\n<\/ol>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u94a9\u5b50\uff1a<\/p>\n<pre><code class=\"language-php line-numbers\">$hook['pre_controller'] = array(\n    'class'    =&gt; 'MyClass',\n    'function' =&gt; 'Myfunction',\n    'filename' =&gt; 'Myclass.php',\n    'filepath' =&gt; 'hooks',\n    'params'   =&gt; array('beer', 'wine', 'snacks')\n);\n<\/code><\/pre>\n<p>CI\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u6302\u94a9\u70b9\uff0c\u76f8\u5f53\u4e8e\u6ce8\u518c\u4e8b\u4ef6\uff0c\u8fd9\u4e00\u6b65\u662fCI\u7cfb\u7edf\u505a\u597d\u4e86\u7684\uff0c\u4e0d\u9700\u8981\u5f00\u53d1\u8005\u81ea\u5df1\u6ce8\u518c\u3002<\/p>\n<ul>\n<li>pre_system \u5728\u7cfb\u7edf\u6267\u884c\u7684\u65e9\u671f\u8c03\u7528\uff0c\u8fd9\u4e2a\u65f6\u5019\u53ea\u6709 \u57fa\u51c6\u6d4b\u8bd5\u7c7b \u548c \u94a9\u5b50\u7c7b \u88ab\u52a0\u8f7d\u4e86\uff0c \u8fd8\u6ca1\u6709\u6267\u884c\u5230\u8def\u7531\u6216\u5176\u4ed6\u7684\u6d41\u7a0b<\/li>\n<li>pre_controller \u5728\u4f60\u7684\u63a7\u5236\u5668\u8c03\u7528\u4e4b\u524d\u6267\u884c\uff0c\u6240\u6709\u7684\u57fa\u7840\u7c7b\u90fd\u5df2\u52a0\u8f7d\uff0c\u8def\u7531\u548c\u5b89\u5168\u68c0\u67e5\u4e5f\u5df2\u7ecf\u5b8c\u6210\u3002<\/li>\n<li>post_controller_constructor \u5728\u4f60\u7684\u63a7\u5236\u5668\u5b9e\u4f8b\u5316\u4e4b\u540e\u7acb\u5373\u6267\u884c\uff0c\u63a7\u5236\u5668\u7684\u4efb\u4f55\u65b9\u6cd5\u90fd\u8fd8\u5c1a\u672a\u8c03\u7528\u3002<\/li>\n<li>post_controller \u5728\u4f60\u7684\u63a7\u5236\u5668\u5b8c\u5168\u8fd0\u884c\u7ed3\u675f\u65f6\u6267\u884c\u3002<\/li>\n<li>display_override \u8986\u76d6 _display() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u5728\u7cfb\u7edf\u6267\u884c\u7ed3\u675f\u65f6\u5411\u6d4f\u89c8\u5668\u53d1\u9001\u6700\u7ec8\u7684\u9875\u9762\u7ed3\u679c\u3002 \u8fd9\u53ef\u4ee5\u8ba9\u4f60\u6709\u81ea\u5df1\u7684\u663e\u793a\u9875\u9762\u7684\u65b9\u6cd5\u3002<\/li>\n<li>cache_override \u4f7f\u7528\u4f60\u81ea\u5df1\u7684\u65b9\u6cd5\u6765\u66ff\u4ee3 \u8f93\u51fa\u7c7b \u4e2d\u7684 _display_cache() \u65b9\u6cd5\uff0c\u8fd9\u8ba9\u4f60\u6709\u81ea\u5df1\u7684\u7f13\u5b58\u663e\u793a\u673a\u5236\u3002<\/li>\n<li>post_system \u5728\u6700\u7ec8\u7684\u9875\u9762\u53d1\u9001\u5230\u6d4f\u89c8\u5668\u4e4b\u540e\u3001\u5728\u7cfb\u7edf\u7684\u6700\u540e\u671f\u88ab\u8c03\u7528\u3002<\/li>\n<\/ul>\n<h3>3.Hooks\u7684\u5b9e\u73b0<\/h3>\n<pre><code class=\"language-php line-numbers\">class CI_Hooks {\n\n    \/**\n     * Determines whether hooks are enabled\n     *\n     * @var bool\n     *\/\n    public $enabled = FALSE;\n\n    \/**\n     * List of all hooks set in config\/hooks.php\n     *\n     * @var array\n     *\/\n    public $hooks = array();\n\n    \/**\n     * Array with class objects to use hooks methods\n     *\n     * @var array\n     *\/\n    protected $_objects = array();\n\n    \/**\n     * In progress flag\n     *\n     * Determines whether hook is in progress, used to prevent infinte loops\n     *\n     * @var bool\n     *\/\n    protected $_in_progress = FALSE;\n\n    \/**\n     * Class constructor\n     *\n     * @return  void\n     *\/\n    public function __construct()\n    {\n        $CFG =&amp; load_class('Config', 'core');\n        log_message('info', 'Hooks Class Initialized');\n\n        \/\/ If hooks are not enabled in the config file\n        \/\/ there is nothing else to do\n        if ($CFG-&gt;item('enable_hooks') === FALSE)\n        {\n            return;\n        }\n\n        \/\/ Grab the \"hooks\" definition file.\n        if (file_exists(APPPATH.'config\/hooks.php'))\n        {\n            include(APPPATH.'config\/hooks.php');\n        }\n\n        if (file_exists(APPPATH.'config\/'.ENVIRONMENT.'\/hooks.php'))\n        {\n            include(APPPATH.'config\/'.ENVIRONMENT.'\/hooks.php');\n        }\n\n        \/\/ If there are no hooks, we're done.\n        if ( ! isset($hook) OR ! is_array($hook))\n        {\n            return;\n        }\n\n        $this-&gt;hooks =&amp; $hook;\n        $this-&gt;enabled = TRUE;\n    }\n\n    \/\/ --------------------------------------------------------------------\n\n    \/**\n     * Call Hook\n     *\n     * Calls a particular hook. Called by CodeIgniter.php.\n     *\n     * @uses    CI_Hooks::_run_hook()\n     *\n     * @param   string  $which  Hook name\n     * @return  bool    TRUE on success or FALSE on failure\n     *\/\n    public function call_hook($which = '')\n    {\n        if ( ! $this-&gt;enabled OR ! isset($this-&gt;hooks[$which]))\n        {\n            return FALSE;\n        }\n\n        if (is_array($this-&gt;hooks[$which]) &amp;&amp; ! isset($this-&gt;hooks[$which]['function']))\n        {\n            foreach ($this-&gt;hooks[$which] as $val)\n            {\n                $this-&gt;_run_hook($val);\n            }\n        }\n        else\n        {\n            $this-&gt;_run_hook($this-&gt;hooks[$which]);\n        }\n\n        return TRUE;\n    }\n\n    \/\/ --------------------------------------------------------------------\n\n    \/**\n     * Run Hook\n     *\n     * Runs a particular hook\n     *\n     * @param   array   $data   Hook details\n     * @return  bool    TRUE on success or FALSE on failure\n     *\/\n    protected function _run_hook($data)\n    {\n        \/\/ Closures\/lambda functions and array($object, 'method') callables\n        if (is_callable($data))\n        {\n            is_array($data)\n                ? $data[0]-&gt;{$data[1]}()\n                : $data();\n\n            return TRUE;\n        }\n        elseif ( ! is_array($data))\n        {\n            return FALSE;\n        }\n\n        \/\/ -----------------------------------\n        \/\/ Safety - Prevents run-away loops\n        \/\/ -----------------------------------\n\n        \/\/ If the script being called happens to have the same\n        \/\/ hook call within it a loop can happen\n        if ($this-&gt;_in_progress === TRUE)\n        {\n            return;\n        }\n\n        \/\/ -----------------------------------\n        \/\/ Set file path\n        \/\/ -----------------------------------\n\n        if ( ! isset($data['filepath'], $data['filename']))\n        {\n            return FALSE;\n        }\n\n        $filepath = APPPATH.$data['filepath'].'\/'.$data['filename'];\n\n        if ( ! file_exists($filepath))\n        {\n            return FALSE;\n        }\n\n        \/\/ Determine and class and\/or function names\n        $class      = empty($data['class']) ? FALSE : $data['class'];\n        $function   = empty($data['function']) ? FALSE : $data['function'];\n        $params     = isset($data['params']) ? $data['params'] : '';\n\n        if (empty($function))\n        {\n            return FALSE;\n        }\n\n        \/\/ Set the _in_progress flag\n        $this-&gt;_in_progress = TRUE;\n\n        \/\/ Call the requested class and\/or function\n        if ($class !== FALSE)\n        {\n            \/\/ The object is stored?\n            if (isset($this-&gt;_objects[$class]))\n            {\n                if (method_exists($this-&gt;_objects[$class], $function))\n                {\n                    $this-&gt;_objects[$class]-&gt;$function($params);\n                }\n                else\n                {\n                    return $this-&gt;_in_progress = FALSE;\n                }\n            }\n            else\n            {\n                class_exists($class, FALSE) OR require_once($filepath);\n\n                if ( ! class_exists($class, FALSE) OR ! method_exists($class, $function))\n                {\n                    return $this-&gt;_in_progress = FALSE;\n                }\n\n                \/\/ Store the object and execute the method\n                $this-&gt;_objects[$class] = new $class();\n                $this-&gt;_objects[$class]-&gt;$function($params);\n            }\n        }\n        else\n        {\n            function_exists($function) OR require_once($filepath);\n\n            if ( ! function_exists($function))\n            {\n                return $this-&gt;_in_progress = FALSE;\n            }\n\n            $function($params);\n        }\n\n        $this-&gt;_in_progress = FALSE;\n        return TRUE;\n    }\n\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u94a9\u5b50\u662f\u4ec0\u4e48\uff1f \u94a9\u5b50\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5904\u7406\u6d88\u606f\u7684\u7a0b\u5e8f\u6bb5\uff0c\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\uff0c\u628a\u5b83\u6302\u5165\u7cfb\u7edf\u3002\u6bcf\u5f53\u7279\u5b9a\u7684\u6d88\u606f\u53d1\u51fa\uff0c\u5728\u6ca1\u6709\u5230\u8fbe [&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-980","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\/980","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=980"}],"version-history":[{"count":1,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/980\/revisions"}],"predecessor-version":[{"id":981,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/980\/revisions\/981"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}