Thinkphp is a free open source, fast, simple object-oriented lightweight PHP development framework, this article mainly introduces the use of THINKPHP5 behavior Summary, the need for friends can refer to, hope to help everyone.
Under the application or module to build behavior directory, directory built behavior class file test.php (following the home module as an example)
Namespace app/home/behavior;//Note the different namespaces for the application or module class Test {public function run (& $params) { echo $params; }}
Method One
1. In the entry file, add: Define (' App_hook ', true);
2.home module built tags. PHP file definition behavior
return [ ' app_init ' = [' app//home//behavior//test ',//notice the behavior of the namespace (same as below)], ' zzzzz ' = [//Custom tab bit ' app// Home//behavior//test ',],//........);
3. Local interception behavior required within the Controller method
/think/hook::listen (' App_init ', $params);//parameter is variable (hereinafter)/think/hook::listen (' zzzzz ', $params);//Custom
Method Two
Common Files for 1.home modules (common. PHP) or a controller method within the binding behavior to the label bit
/think/hook::add (' App_init ', ' app//home//behavior//test '); /think/hook::add (' zzzz ', ' app//home//behavior//test ');//Custom
2. Local interception behavior required within the Controller method
/think/hook::listen (' App_init ', $params); /think/hook::listen (' zzzzz ', $params);//Custom
Method Three
Direct execution of the behavior where required within the Controller method
/think/hook::exec (' app//home//behavior//test ', ' Run ', $params);
Method Four
Using the closure function 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 binds 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 '; }}
Common Files for 2.home modules (common. PHP) or a controller method within the binding behavior to the label bit
/think/hook::add (' App_init ', ' app//home//behavior//test ');/think/hook::add (' App_end ', ' app//home//behavior// Test ');
3. Local interception behavior required within the Controller method
/think/hook::listen (' App_init ', $params); /think/hook::listen (' App_end ', $params);