1、创建钩子行为
我们自己定义的标签位可以直接放在Think\Behaviors中,也可以放在应用目录中,比如说Home模块下,新建一个Behaviors的文件夹,在文件夹内新建:
标签名+Behavior.class.PHP
注:需要带Behavior的原因,见代码:
static public function exec($name, $tag,&$params=NULL) { if('Behavior' == substr($name,-8) ){ // 行为扩展必须用run入口方法 $tag = 'run'; } $addon = new $name(); return $addon->$tag($params); }
相关推荐:《ThinkPHP教程》
在这里我自己自定义的标签名是My
namespace Behavior; use Think\Behavior; class MyBehavior extends Behavior { public function run(&$arg){ echo 'Thinkphp 中的'.$arg['name'].'功能,'.$arg['value'].'中...'; } }
注意类名大小写
2、将钩子添加进钩子集中
方法一(手动注册):直接在控制器中添加:
Hook::add('addd','Behavior\\adBehavior');
方法二(自动注册):
在Conf文件夹里面(完整路径D:\think\application\Home\Conf\tags.php,当然这是我的情况)tags.php的内容:
return array( //'action_begin'=>array('Home\\Behaviors\\test','Home\\Behaviors\\test'), //一个标签位可以有多个行为,使用数组即可。 // 如果是3.2.1版本 则需要改成 // 'action_begin'=>array('Home\\Behaviors\\testBehavior','Home\\Behaviors\\testBehavior'), 'my'=>array('Behaviors\\MyBehavior') );
3、添加监听(我这里才用模板中直接监听使用)
此处如果报找不到hook方法,请在ThinkPHP/Common/functions.php中添加(当然也可以在其他公共文件):
function hook($hook,$params= array()){ \Think\Hook::listen($hook,$params); }
最后在模板中使用:
{:hook('my', array('name'=>'钩子','value'=>'学习'))}
The above is the detailed content of How to use thinkphp5 hooks. For more information, please follow other related articles on the PHP Chinese website!

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


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

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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