search
HomeDatabaseMysql TutorialThinkPHP3.2 扩展

之前写到TP3.1的行为扩展是tag();在TP3.2中引入了另一种说法—:钩子。 我们来看一下TP3.2中的钩子这个东西: 一:文件流程: 1:/index.php - require './ThinkPHP/ThinkPHP.php'; 2:/ThinkPHP/ThinkPHP.php—- require CORE_PATH.'Think'.EXT; Think\Think:

之前写到TP3.1的行为扩展是tag();在TP3.2中引入了另一种说法—:钩子。
我们来看一下TP3.2中的钩子这个东西:
一:文件流程:
1:/index.php ->require './ThinkPHP/ThinkPHP.php';
2:/ThinkPHP/ThinkPHP.php—->require CORE_PATH.'Think'.EXT; Think\Think::start();
3:/ThinkPHP/Library/Think/Think.class.php—–>App::run();
4:/ThinkPHP/Library/Think/App.class.php 。到这里基本流程就走完了,(这里不说细节);

二:代码:
1:看一下 App::run()方法:

<code>        
        Hook<span>::listen(<span>'app_init'</span>);</span>
        App<span>::init();</span>
        
        Hook<span>::listen(<span>'app_begin'</span>);</span>
        <span>// Session初始化</span>
        <span>if(!IS_CLI)</span>{
            <span>session(<span>C(<span>'SESSION_OPTIONS'</span>)</span>)</span>;
        }
        <span>// 记录应用初始化时间</span>
        <span>G(<span>'initTime'</span>)</span>;
        App<span>::exec();</span>
        
        Hook<span>::listen(<span>'app_end'</span>);</span>
        return ;</code>

其中的Hook::listen(”)就是用来执行钩子的,我们可以在app_init这个安插的位置用来获取应用中安装的插件。

看一下Hook::listen();

<code><span><span> @param</span><span> @param</span> mixed $params 传入参数
     *<span> @return</span> void
     */</span>
    <span>static</span> <span>public</span> <span><span>function</span> <span>listen</span><span>(<span>$tag</span>, &<span>$params</span>=NULL)</span> {</span>
        <span>if</span>(<span>isset</span>(<span>self</span>::<span>$tags</span>[<span>$tag</span>])) {
            <span>if</span>(APP_DEBUG) {
                G(<span>$tag</span>.<span>'Start'</span>);
                trace(<span>'[ '</span>.<span>$tag</span>.<span>' ] --START--'</span>,<span>''</span>,<span>'INFO'</span>);
            }
            <span>foreach</span> (<span>self</span>::<span>$tags</span>[<span>$tag</span>] <span>as</span> <span>$name</span>) {
                APP_DEBUG && G(<span>$name</span>.<span>'_start'</span>);
                <span>$result</span> =   <span>self</span>::exec(<span>$name</span>, <span>$tag</span>,<span>$params</span>);
                <span>if</span>(APP_DEBUG){
                    G(<span>$name</span>.<span>'_end'</span>);
                    trace(<span>'Run '</span>.<span>$name</span>.<span>' [ RunTime:'</span>.G(<span>$name</span>.<span>'_start'</span>,<span>$name</span>.<span>'_end'</span>,<span>6</span>).<span>'s ]'</span>,<span>''</span>,<span>'INFO'</span>);
                }
                <span>if</span>(<span>false</span> === <span>$result</span>) {
                    <span>// 如果返回false 则中断插件执行</span>
                    <span>return</span> ;
                }
            }
            <span>if</span>(APP_DEBUG) { <span>// 记录行为的执行日志</span>
                trace(<span>'[ '</span>.<span>$tag</span>.<span>' ] --END-- [ RunTime:'</span>.G(<span>$tag</span>.<span>'Start'</span>,<span>$tag</span>.<span>'End'</span>,<span>6</span>).<span>'s ]'</span>,<span>''</span>,<span>'INFO'</span>);
            }
        }
        <span>return</span>;
    }</code>

其中关键是:self::exec($name, $tag,$params); 看一下exec的代码:

<code> <span>/**
     * 执行某个插件
     *<span> @param</span> string $name 插件名称
     *<span> @param</span><span> @param</span> Mixed $params 传入的参数
     *<span> @return</span> void
     */</span>
    <span>static</span> <span>public</span> <span><span>function</span> <span>exec</span><span>(<span>$name</span>, <span>$tag</span>,&<span>$params</span>=NULL)</span> {</span>
        <span>if</span>(<span>false</span> === strpos(<span>$name</span>,<span>'\\'</span>)) {
            <span>// 插件(多个入口)</span>
            <span>$class</span>   =  <span>"Addons\\{$name}\\{$name}Addon"</span>;
        }<span>else</span>{
            <span>// 行为扩展(只有一个run入口方法)</span>
            <span>$class</span>   =  <span>$name</span>.<span>'Behavior'</span>;
            <span>$tag</span>    =   <span>'run'</span>;
        }
        <span>$addon</span>   = <span>new</span> <span>$class</span>();
        <span>return</span> <span>$addon</span>-><span>$tag</span>(<span>$params</span>);
    }</code>

最后还不是 new $class();进而return $addon->$tag($params); 又转到了具体钩子的代码方法。其实就是我们原本的调用class的方法,只不过经过别人的高度封装了。

三:那么问题来了,这个钩子有什么用呢? 怎么用?
这里以OneThink 的{:hook('AdminIndex')}为例,看一些别人是怎么用的。
在系统初始化到 Hook::listen('app_init'); 时,

<code><span><span><?php </span>
<span>return</span> <span>array</span>(
    <span>'app_init'</span>=><span>array</span>(<span>'Common\Behavior\InitHook'</span>)
);</span></span></code>

一看就明白,无非就是读取持久化的信息,放到缓存或是其他的方式

<code> <span>// 行为扩展的执行入口必须是run</span>
    <span>public</span> <span><span>function</span> <span>run</span><span>(&<span>$content</span>)</span>{</span>
        <span>if</span>(<span>isset</span>(<span>$_GET</span>[<span>'m'</span>]) && <span>$_GET</span>[<span>'m'</span>] === <span>'Install'</span>) <span>return</span>;

        <span>$data</span> = S(<span>'hooks'</span>);
        <span>if</span>(!<span>$data</span>){
            <span>$hooks</span> = M(<span>'Hooks'</span>)->getField(<span>'name,addons'</span>);
            <span>foreach</span> (<span>$hooks</span> <span>as</span> <span>$key</span> => <span>$value</span>) {
                <span>if</span>(<span>$value</span>){
                    <span>$map</span>[<span>'status'</span>]  =   <span>1</span>;
                    <span>$names</span>          =   explode(<span>','</span>,<span>$value</span>);
                    <span>$map</span>[<span>'name'</span>]    =   <span>array</span>(<span>'IN'</span>,<span>$names</span>);
                    <span>$data</span> = M(<span>'Addons'</span>)->where(<span>$map</span>)->getField(<span>'id,name'</span>);
                    <span>if</span>(<span>$data</span>){
                        <span>$addons</span> = array_intersect(<span>$names</span>, <span>$data</span>);
                        Hook::add(<span>$key</span>,<span>$addons</span>);
                    }
                }
            }
            S(<span>'hooks'</span>,Hook::get());
        }<span>else</span>{
            Hook::import(<span>$data</span>,<span>false</span>);
        }
    }</code>

当在程序执行到{:hook(‘AdminIndex’)}时—>调用的是Hook::listen(‘AdminIndex’);
AdminIndex这个挂载点包含了三个插件:分别是:SiteStat, SystemInfo,DevTeam。
用一个循环来分别按顺序执行.

总结:钩子其实就是起到一个挂载点的作用,这个钩子挂在哪里,就可以在哪里执行,内容或功能就是挂载插件或类库的具体实现。这样实现的代码就有很大的灵活性,挂载点不变,挂的东西变量,功能也就相应的变化,是不是很灵活强大呀。


以上只属于学习笔记,若有错请指正。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php如何使用PHP的SNMP扩展?php如何使用PHP的SNMP扩展?Jun 02, 2023 am 10:22 AM

PHP的SNMP扩展是一种使PHP能够通过SNMP协议与网络设备进行通信的扩展程序。使用该扩展可以方便地获取和修改网络设备的配置信息,例如路由器、交换机等设备的CPU、内存、网络接口等信息,也可以进行诸如开关设备端口等控制操作。本文将介绍SNMP协议的基础知识、PHP的SNMP扩展的安装方法以及如何在PHP中使用SNMP扩展进行网络设备的监控和控制。一、SN

如何使用极光推送扩展,在PHP应用中实现批量消息推送功能如何使用极光推送扩展,在PHP应用中实现批量消息推送功能Jul 25, 2023 pm 08:07 PM

如何使用极光推送扩展,在PHP应用中实现批量消息推送功能在移动应用的开发中,消息推送是一项非常重要的功能。极光推送是一种常用的消息推送服务,提供了丰富的功能和接口。本文将介绍如何使用极光推送扩展在PHP应用中实现批量消息推送功能。第一步:注册极光推送账号并获取API密钥首先,我们需要在极光推送官网(https://www.jiguang.cn/push)注册

php如何使用PHP的ZipArchive扩展?php如何使用PHP的ZipArchive扩展?Jun 02, 2023 am 08:13 AM

PHP是一种流行的服务器端语言,可以用来开发Web应用程序和处理文件。PHP的ZipArchive扩展是一个强大的工具,可以在PHP中操作zip文件。在这篇文章中,我们将介绍如何使用PHP的ZipArchive扩展来创建、读取和修改zip文件。一、安装ZipArchive扩展在使用ZipArchive扩展之前,需要确保已经安装了这个扩展。安装方法如下:1.安

php如何使用PHP的Phar扩展?php如何使用PHP的Phar扩展?May 31, 2023 pm 11:31 PM

随着PHP的发展和应用场景的不断扩大,Phar扩展已经成为PHP编程中的重要一环。Phar是PHPArchive的缩写,它可以将多个PHP文件和资源打包成单个文件,方便进行分发和管理。本文将介绍如何使用PHP的Phar扩展来进行打包和管理。安装Phar扩展首先,我们需要检查PHP是否已经安装Phar扩展。在Linux下,通过终端输入以下命令:php-m

php如何使用PHP的POSIX扩展?php如何使用PHP的POSIX扩展?Jun 03, 2023 am 08:01 AM

PHP的POSIX扩展是一组允许PHP与POSIX兼容操作系统进行交互的函数和常量。POSIX(PortableOperatingSystemInterface)是一组操作系统接口标准,旨在允许软件开发人员编写可在各种UNIX或UNIX类操作系统上运行的应用程序。本文将介绍如何使用PHP的POSIX扩展,包括安装和使用。一、安装PHP的POSIX扩展在

教程:使用百度云推送(Baidu Push)扩展在PHP应用中实现消息推送功能教程:使用百度云推送(Baidu Push)扩展在PHP应用中实现消息推送功能Jul 26, 2023 am 09:25 AM

教程:使用百度云推送(BaiduPush)扩展在PHP应用中实现消息推送功能引言:随着移动应用的迅猛发展,消息推送功能在应用程序中变得越来越重要。为了实现即时通知和消息推送功能,百度提供了一种强大的云推送服务,即百度云推送(BaiduPush)。在本教程中,我们将学习如何使用百度云推送扩展(PHPSDK)在PHP应用中实现消息推送功能。我们将使用百度云

Vue如何实现组件的复用和扩展?Vue如何实现组件的复用和扩展?Jun 27, 2023 am 10:22 AM

随着前端技术的不断发展,Vue已经成为了前端开发中的热门框架之一。在Vue中,组件是其中的核心概念之一,它可以将页面分解为更小,更易管理的部分,从而提高开发效率和代码复用性。本文将重点介绍Vue如何实现组件的复用和扩展。一、Vue组件复用mixinsmixins是Vue中的一种共享组件选项的方式。Mixins允许将多个组件的组件选项合并成一个对象,从而最大

PHP和WebDriver扩展:如何模拟用户点击和输入操作PHP和WebDriver扩展:如何模拟用户点击和输入操作Jul 07, 2023 pm 05:10 PM

PHP和WebDriver扩展:如何模拟用户点击和输入操作近年来,随着Web应用程序的快速发展,自动化测试变得越来越重要。在自动化测试中,模拟用户操作是一个关键的环节,它可以使我们更准确地测试和验证我们的应用程序。在PHP开发中,我们通常使用SeleniumWebDriver来实现自动化测试。SeleniumWebDriver是一种强大的工具,它可以模拟

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),