thinkphp 插件


1.切換到項目根目錄,使用composer require 5ini99/think-addons:dev-master命令安裝thinkphp插件

  如果是root用戶或是管理員執行的話會有提示

  等一下,會有這個提示

  

 這就表示下好了,然后訪問tp5,會在根目錄生成addons文件夾(如果tp5目錄沒有權限的話需要改變權限)

 編輯application/extra/addons.php

    

  在addons文件新建test文件夾,在test下新建Test.php

<?php
namespace addons\test;    // 注意命名空間規范

use think\Addons;

/**
 * 插件測試
 * @author byron sampson
 */
class Test extends Addons    // 需繼承think\addons\Addons類
{
    // 該插件的基礎信息 也可以單獨配置在info.ini中
    public $info = [
        'name' => 'test',    // 插件標識
        'title' => '插件測試',    // 插件名稱
        'description' => 'tp5插件測試',    // 插件簡介
        'status' => 0,    // 狀態
        'author' => 'byron sampson',
        'version' => '0.1'
    ];

    /**
     * 插件安裝方法
     * @return bool
     */
    public function install()
    {
       // return true;
    }

    /**
     * 插件卸載方法
     * @return bool
     */
    public function uninstall()
    {
       // return true;
    }

    /**
     * 實現的testhook鈎子方法
     * @return mixed
     */
    public function testhook($param)
    {
        // 調用鈎子時候的參數信息
        // print_r($param);
        // 當前插件的配置信息,配置信息存在當前目錄的config.php文件中,見下方
        // print_r($this->getConfig());
        // 可以返回模板,模板文件默認讀取的為插件目錄中的文件。模板名不能為空!
        //return $this->fetch('info');
     echo "這里是鈎子調用的方法";
    }

}
View Code

 

 在test下新建config.php ,info.ini 可以不寫內容

 從項目的模板中訪問插件,{:hook('testhook')};

 

也可以在插件新建controller目錄,用法與tp5的一樣,目錄下面創建Index控制器

從項目中需要使用addon_url()訪問項目

  index.html內容

 

訪問點擊鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM