How to use instance sharing THINKPHP5 behavior

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.