在 Codeigniter 中 hook 的目的是什么?我们为什么要创造?

In Codeigniter what is the purpose of hook ? Why we have to create?

我是 codeigniter 的新手。在每次面试中都询问了钩子。我不明白什么是 hook 为什么我必须使用它?有什么好处。

将钩子视为 codeigniter 中的中间件实现。 基本上,您可以扩展框架的基本功能,将代码添加到请求生命周期的特定时刻。

这是 codeigniter 2 中可用的挂钩列表

The following is a list of available hook points.

pre_system Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.

pre_controller Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.

post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.

post_controller Called immediately after your controller is fully executed.

display_override Overrides the _display() function, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output()

cache_override Enables you to call your own function instead of the _display_cache() function in the output class. This permits you to use your own cache display mechanism.

post_system Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

这是在 codeigniter

中使用它的示例 link

https://qasimbadami.wordpress.com/2012/05/18/codeigniter-hooks-tutorial/

注意:假设你有一个大项目和将近 100 个控制器,如果每次检查会话是否存在,每个 post 请求 所以不要使用 hook