Recently the use of ThinkPHP5 development and application, before the habit of some plug-in development, and feel good, but now based on the document is not perfect, so their study a little basic functionality. The plug-in function TP3.2 the migration over.
Add plugin method:
1, was added as follows index.php inlet system
// plugins directory of the DEFINE ( 'ADDON_PATH', __DIR__ '/../addons/'.); // turn on the system behavior define ( 'APP_HOOK', true) ;
2. Add addons and plugins table hooks hooks table in the database
TABLE `addons` the CREATE ( ` id` int (10) unsigned the AUTO_INCREMENT the COMMENT the NOT NULL 'master key', `name` VARCHAR (40) the COMMENT the NOT NULL 'widget name or identifier', ` title` VARCHAR (20 is) the NOT NULL the DEFAULT ' 'COMMENT' Chinese name ', `description` the COMMENT text' plug-described ', ` status` tinyint (. 1) the NOT NULL the DEFAULT'. 1 'COMMENT' state ', `config` the COMMENT text' configuration ', ` author` VARCHAR (40 ) DEFAULT '' COMMENT 'OF', `version` VARCHAR (20 is) DEFAULT '' COMMENT 'version number', ` create_time` int (10) unsigned the NOT NULL the DEFAULT '0' the COMMENT 'installation time', `has_adminlist` tinyint ( 1) unsigned NOT NULL DEFAULT '0 ' COMMENT ' Is there a list of background' PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='插件表'; CREATE TABLE `hooks` ( `id` int (10) unsigned NOT NULL AUTO_INCREMENT COMMENT ' master key', ` name` VARCHAR (40) the NOT NULL the DEFAULT '' the COMMENT 'hook name', `description` the COMMENT text the NOT NULL 'Description', ` type` tinyint ( 1) unsigned NOT NULL DEFAULT '1 ' COMMENT ' type', `update_time` int (10) unsigned the NOT NULL the DEFAULT '0' the COMMENT 'update', ` addons` VARCHAR (255) the NOT NULL the DEFAULT '' the COMMENT 'hanging hook the plug-in carrier '', '' division ', `status` tinyint (2) the DEFAULT'. 1 ', a PRIMARY KEY (` id`), UNIQUE KEY `name` (` name`) the USING BTREE ) =. 1 ENGINE = MyISAM the AUTO_INCREMENT DEFAULT CHARSET = utf8;
3. Create addons directory in the root directory of ThinkPHP5
www WEB project directory ├─composer.json composer definition files ├─README.md README file ├─LICENSE.txt authorization documentation ├─addons plugin directory (new) ├─application application directory │ ├─common Public module catalog (you can change) │ ├─runtime when running applications directory (writable, can be customized) │ ├─module module directory │ │ └─ ... more library catalog │ ├─common.php public function file │ ├─config. php public profile │ ├─route.php routing configuration file │ └─database.php database configuration file │ ├─public the WEB directory (External access Catalog) │ ├─index.php entry file │ ├─.htaccess for the apache rewriting │ └─router.php quick test file (for PHP built webserver) │ ├─thinkphp frame system directory
4, add two public methods in the application of a public function in common.php
/ ** * Get the class name of the plugin class * @param strng $ name plugin name * $ ext extension String @param * / function get_addon_class ($ name, $ EXT = EXT) { // initialize the namespace and class name $ class = "$ {name} addons \\ \\" ucfirst ($ name);. return $ class; } / ** * hook processing widget * @param string $ hook hooks name * @param mixed $ params passed parameters * @ void return * / function hook (hook $, $ the params = []) { // hook call \ Think \ :: hook the listen (hook $, $ the params); }
5, created in the application directory common module, create a directory in common behavior behavior module, create Hooks.php hook behavior plug-in directory of public behavior, as follows:
<?php // +---------------------------------------------------------------------- // | zzstudio [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2016 http://www.zzstudio.net All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: Byron Sampson <[email protected]> // +---------------------------------------------------------------------- namespace app\common\behavior; use think\Hook; class Hooks { RUN function public (param = & $ []) { IF (defined ( 'BIND_MODULE') && BIND_MODULE === 'the Install') return; // dynamically join a namespace \ think \ Loader :: addNamespace ( ' addons', ADDON_PATH ); // get the data hook $ data = S ( 'hooks'); ! IF ($ data) { $ M = hooks ( 'hooks') -> getField ( 'name, addons'); // get implemented hook widget information of the foreach ($ Hooks AS $ Key => $ value) { IF ($ value) { $ Map [ 'Status'] =. 1; $ names = the explode (', ', $ value); $ Map [ 'name' ] = [ 'IN', $ names]; $ Data = M (' addons ') -> where ($ map) -> getField ('id,name'); if($data){ $addons = array_intersect($names, $data); Hook::add($key, array_map('get_addon_class', $addons)); } } } S('hooks', Hook::get()); }else{ Hook::import($data, false); } } }
6, create tags.php behavior in the application directory, configuration files, and add content
<? PHP // define the behavior of the system return [ 'app_init' => [ 'App behavior \\ \\ \\ Common Hooks' ], 'action_begin' => [ ], 'app_end' => [ ] ];
So far, plug-in function is integrated is completed.
Next we try to be a simple plug-in:
1, create a base class Base.php plug-in addons directory
<?php // +---------------------------------------------------------------------- // | addons [ WE CAN DO IT JUST ZZSTUDIO IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2015 http://www.zzstudio.net All rights reserved. // +---------------------------------------------------------------------- // | Author: byron sampson <[email protected]> // +---------------------------------------------------------------------- namespace addons; /** * 插件类 * @author byron sampson <[email protected]> */ abstract class Base{ /** * 视图实例对象 * @var view Protected @access * /** * / Protected $ View = null; / ** * $ info = Array ( * 'name' => 'Editor', * 'title' => 'editor', * 'Description' => 'station for enhancing whole long text input and display ', *' Status' =>. 1, * 'author' => 'thinkphp', * 'Version' => '0.1' *) * / public $ info = Array (); public $ addon_path = ''; public config_file $ = ''; public custom_config $ = ''; public $ admin_list = Array (); public custom_adminlist $ = ''; Array = $ access_url public (); * Base constructor. * 基类构造函数 */ public function __construct() { $this->view = new \think\View(); $this->addon_path = ADDON_PATH . $this->getName() . '/'; $TMPL_PARSE_STRING = C('parse_str'); $TMPL_PARSE_STRING['__ADDONROOT__'] = $TMPL_PARSE_STRING['__ADDONS__'] . '/' . $this->getName(); C('parse_str', $TMPL_PARSE_STRING); if(is_file($this->addon_path . 'config.php')){ $this->config_file = $this->addon_path . 'config.php'; * @return Action * @param String $ Theme Template theme * @access protected * Template Theme Set / ** } } * / Final Theme function protected (Theme $) { $ this-> View-> Theme ($ Theme); return $ the this; } / ** * Template variable assignment * @access protected * @param Mixed $ template name to be displayed variable @param mixed $ value the value of variable * * @return the Action * / Final protected function ASSIGN (name $, $ value = '') { $ this-> View-> ASSIGN (name $, $ value); return $ the this ; } a method for displaying a template // Final FETCH protected function ($ TemplateFile = CONTROLLER_NAME) { IF {(is_file (TemplateFile $)!) $templateFile = $this->addon_path . $templateFile . ".html"; if(!is_file($templateFile)){ E(L('_TEMPLATE_NOT_EXIST_') . ":$templateFile"); } } return $this->view->fetch($templateFile); } /** * 获取插件名 * @return string */ final public function getName() { $class = get_class($this); list($space, $name, $class) = explode('\\', $class); return $name; Final CheckInfo public function () * / * @return BOOL * check the configuration information is complete / ** } { $info_check_keys = array('name','title','description','status','author','version'); foreach ($info_check_keys as $value) { if(!array_key_exists($value, $this->info)) return false; } return true; } /** * 获取插件的配置数组 */ final public function getConfig($name = '') { static $_config = []; if(empty($name)){ $name = $this->getName(); } if(isset($_config[$name])){ return $_config[$name]; } $map['name'] = $name; $map['status'] = 1; $config = M('Addons')->where($map)->getField('config'); if($config){ $config = json_decode($config, true); }else{ $config = []; $temp_arr = include $this->config_file; foreach ($temp_arr as $key => $value) { if($value['type'] == 'group'){ foreach ($value['options'] as $gkey => $gvalue) { foreach ($gvalue['options'] as $ikey => $ivalue) { $config[$ikey] = $ivalue['value']; } } }else{ config $ [Key $] = $ temp_arr [$ Key] [ 'value']; } } } $_config[$name] = $config; return $ config; } / ** * must be installed to achieve * Mixed @return * / abstract public the install function (); / ** * The method must be uninstalled plug * Mixed @return * / abstract public uninstall function (); }
2, plug-in table (addons), hook table (Hooks) adding the corresponding data
INSERT INTO `addons` (`id`, `name`, `title`, `description`, `status`, `config`, `author`, `version`, `create_time`, `has_adminlist`) VALUES ('2', 'test', 'test插件', 'test插件简介', '1', NULL, 'byron sampson', '0.1', '1438154545', '0'); INSERT INTO .`hooks` (`id`, `name`, `description`, `type`, `update_time`, `addons`, `status`) VALUES ('21', 'demo', 'demo钩子', '1', '1384481614', 'test', '1');
3. Create a test directory addons directory, and create a Test.php internal plug-in class
<?php // +---------------------------------------------------------------------- // | test [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2016 http://www.zzstudio.net All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: Byron Sampson <[email protected]> // +---------------------------------------------------------------------- namespace addons\test; class Test extends \addons\Base { /** * Achieve demo hook * @param the params Array $ * / public function demo (the params = $ []) { echo 'demo Hook Page IS!' $ The params [ 'P'];. } / ** * installation method * / public the install function () { // the TODO:. Implement the install () method } / ** * uninstall * / public uninstall function () { // the TODO: Implement uninstall () method. } }
4, in the controller to use hooks for hook method
hook ( 'demo', [ ' p' => 'page']);
this switched: https://qqnin.com/35_81.html