English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Create a behavior directory under the application or module, and build a behavior class file Test.PHP inside the directory (here taking the home module as an example)
namespace app/home/behavior;//Note the different namespaces of the application or module class Test { public function run(&$params) { echo $params; }
Method One
1In the entry file, add: define('APP_HOOK', true);
2Define the behavior in tags.PHP under the .home module
return [ 'app_init' => [ 'app//home//behavior//Test', //Note the namespace of the behavior (as follows) ], 'zzzzz' => [//Custom tag position 'app//home//behavior//Test', ], //................. ];
3Listen to the behavior within the controller method where needed
/think/Hook::listen('app_init', $params);//The parameter is a variable (as follows) /think/Hook::listen('zzzzz', $params);//Custom
Method Two
1.The common file (common.PHP) of the .home module or bind behavior to tag positions within the controller method
/think/Hook::add('app_init', 'app//home//behavior//Test'); /think/Hook::add('zzzz', 'app//home//behavior//Test');//Custom
2Listen to the behavior within the controller method where needed
/think/Hook::listen('app_init', $params); /think/Hook::listen('zzzzz', $params);//Custom
Method Three
Execute the behavior directly at the required place within the controller method
/think/Hook::exec('app}}//home//behavior//Test', 'run', $params);
Method Four
Use closure functions directly within the controller method
$a='Closure function'; /think/Hook::add('zzzz', function($a){ var_dump($a);}); /think/Hook::listen('zzzz', $a);
Method Five
Behavior class bound to multiple tags
1.Test.PHP is defined as follows:
namespace app/home/behavior; class Test { public function app_init(&$params){ echo 'app_init'; } public function app_end(&$params){ echo 'app_end'; }
2.The common file (common.PHP) of the .home module or bind behavior to tag positions within the controller method
/think/Hook::add('app_init', 'app//home//behavior//Test'); /think/Hook::add('app_end', 'app//home//behavior//Test');
3Listen to the behavior within the controller method where needed
/think/Hook::listen('app_init', $params); /think/Hook::listen('app_end', $params);
Summary
The above is what the editor introduces to everyone about Thinkphp5Summary of usage methods, hoping it will be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time. Thank you very much for your support of the Yelling Tutorial website!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, nor has it been manually edited, nor does it assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @ when sending an email), and provide relevant evidence. Once verified, this site will immediately delete the infringing content.