CodeIgniter钩子用法实例详解_PHP
本文实例讲述了CodeIgniter钩子用法。分享给大家供大家参考,具体如下:
CodeIgniter执行流程图中有7个椭圆的框,这表示在执行的过程中埋了7个钩子。那先来看看什么是钩子?
网上找到的两段文字:
钩子的完整实现应该叫事件驱动。事件驱动分为两个阶段,第一个阶段是注册事件,目的是给未来可能发生的“事件”起一个名字,简单的实现方法是用单例模式产生一个持久的对象或者注册一个全局变量,然后将事件名称,以及该事件对应的类与方法插入全局变量即可。也就是挂载一个钩子。
第二个阶段是触发事件,本质上就是在事件的全局变量中查询要触发的事件名称,然后找到注册好的类与方法,实例化并运行。这样子就可以摆脱传统方式中程序必须按顺序的规则,进一步实现解除耦合的目的。
钩子函数可以截获并处理其他应用程序的消息。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获该消息,亦即钩子函数先得到控制权。这时钩子函数即可以加工处理(改变)该消息,也可以不作处理而继续传递该消息,还可以强制结束消息的传递。
可以看到:
1、钩子函数是预设并在特定的条件下触发的。
2、钩子函数接管程序后可以影响到程序的走向。
CI预设钩子
CI中设定了7个钩子,钩子与钩子之间相对独立。前3个钩子是在实例化之前设定的,意味着无法使用get_instance实例,要使用已实例化的类需要global。7个钩子的作用手册上说的比较清楚了,可以重写缓存输出、可以对输出进行处理等。
如何触发CI钩子?
CI在配置文件中设置了钩子开关,要使用钩子首先得打开该开关,然后配置config/hook.php中的hook数组,比如设置post_controller_constructor钩子:
$hook['post_controller_constructor'] = array( 'class' => 'Hook', 'function' => 'post_controller_constructor', 'filename' => 'Hook.php', 'filepath' => 'hooks', 'params' => array('beer', 'wine', 'snacks') );
当控制器实例化之后,调用action之前就会触发去执行hooks目录下的Hook.php中的post_controller_constructor方法。我们可以在该方法中做一些处理。
钩子的应用
到目前为止还未体会到CI钩子的绝妙之处,查看网上有一些应用CI钩子来实现权限控制的程序,即在post_controller_constructor做权限判断,由于这个钩子就好比一个构造函数,在构造函数里判断接下来要执行的方法是否有权限,确实可行。但这个功能也完全可以放在MY_Controller中去实现,甚至会更好。因为CI的钩子是全局的,不管是前台还是后台都会启动该钩子,虽然说这个钩子程序可以做判断,但这里判断不一定是最好的。前面提到过MY_Controller中不同模块公用控制器分开,前台后台继承不同的控制器,如果我只需要对后台进行权限控制,完全可以直接在后台公用控制器中来实现,不影响到前台,只对需要的部分做控制。
这里有个非CI中的例子,思维是一样的。合作渠道的用户登录注册功能,有很多的合作方,不同的合作方需要注册的基本资料相同,但每个合作方可能会有一些特殊的字段或者不同的校验方式。
很多时候碰到问题我们都可以放到日常生活中来看。以注册处理逻辑为例,可以看看其中有个流程是不变的。接受参数->注册资料之前处理->注册->注册资料之后处理。变化的是什么?每一步都可能变化,但流程不变化。所以可以对前、后、处理等相关地方设置钩子,把处理的逻辑分发到特定的情况下去,下面有个参考图,具体的如何设置钩子还需要根据项目来:
事实也证明这种方式是可行的,合作方虽然很多,但是也可以分组,上百个注册页面很容易就实现了。所以利用钩子处理这种流程不变,而中间的某个步骤变化多端的需求是很方便的。
简单点说, 钩子就是特定条件下执行一段程序;再简单点,钩子就是实现解除if判断的一种方式。
过多的if判断会导致程序难以阅读和维护,而通过钩子的处理可以让程序更灵活。钩子有一定的触发条件,条件可以是配置、从数据库读取,或者通过一些技术来实现,比如反射等,使用钩子可以达到解耦的目的。
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》和《CI(CodeIgniter)框架进阶教程》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

CodeIgniter is a lightweight PHP framework that uses MVC architecture to support rapid development and simplify common tasks. CodeIgniter5 is the latest version of the framework and offers many new features and improvements. This article will introduce how to use the CodeIgniter5 framework to build a simple web application. Step 1: Install CodeIgniter5 Downloading and installing CodeIgniter5 is very simple, just follow these steps: Download the latest version

As web applications continue to evolve, it is important to develop applications more quickly and efficiently. And, as RESTful API is widely used in web applications, it is necessary for developers to understand how to create and implement RESTful API. In this article, we will discuss how to implement MVC pattern and RESTful API using CodeIgniter framework. Introduction to MVC pattern MVC (Model-Vie

CodeIgniter middleware: Provides secure file upload and download functions Introduction: In the process of web application development, file upload and download are very common functions. However, for security reasons, handling file uploads and downloads often requires additional security measures. CodeIgniter is a popular PHP framework that provides a wealth of tools and libraries to support developers in building secure and reliable web applications. This article will introduce how to use CodeIgniter middleware to implement secure files

With the development of mobile Internet, instant messaging has become more and more important and popular. For many companies, live chat is more like a communication service, providing a convenient communication method that can quickly and effectively solve business problems. Based on this, this article will introduce how to use the PHP framework CodeIgniter to develop a real-time chat application. Understand the CodeIgniter framework CodeIgniter is a lightweight PHP framework that provides a series of simple tools and libraries to help developers quickly

In today's Internet era, a website that is loved by users must have a simple and clear front-end interface and a powerful back-end management system, and the PHP framework CodeIgniter is an excellent framework that allows developers to quickly build a back-end management system. CodeIgniter has the characteristics of lightweight, high efficiency, and easy expansion. This article will be aimed at beginners and explain in detail how to quickly build a backend management system through this framework. 1. Installation and configuration Installation of PHPCodeIgniter is a PHP-based
