<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 插件控制器 插件除了基于前臺與后臺外,還可以作為獨立的模塊。創建控制器后即可賦予插件前臺訪問功能。 ## 創建控制器 控制器放在`controller`文件夾下,以`demo`插件為例,創建首頁控制器,路徑為:addons/demo/controller/Index.php,如下圖 ![](https://img.kancloud.cn/8c/c7/8cc77f51bb2a94ca11905d43e3272c7c_308x128.png) Index.php 代碼 ~~~ <?php namespace addons\demo\controller; use think\addons\Controller; class Index extends Controller { public function index() { return '首頁'; } } ~~~ 瀏覽器訪問地址格式:http://網址/addons/插件名稱/控制器名/操作方法 如上`demo `插件,那么首頁控制器訪問如下,沒寫控制器那么訪問的是`Index`控制器,操作方法默認是`index` http://網址/addons/demo/index/index 首頁默認是Index,操作方法index,那么可以省略,如下即可直接訪問到 http://網址/addons/demo/ ## 基類控制器 創建的控制器繼承`think\addons\Controller`基類,文件位置在:vendor/hkcms/cms-addons/src/addons/Controller.php 繼承之后有以下屬性、方法可以調用,你可根據自己的需要重寫某些屬性、方法 | 屬性名 | 說明 | 使用說明 | | --- | --- | --- | | $error_tmpl | 錯誤頁 | 默認是插件視圖根目錄下的error.html,參考app/common/tpl/error.html | | $success_tmpl | 成功頁 | 同上,只有使用error方法或success方法時有效 | | $app | 容器變量 | 有路由、配置、路由等實例對象,更多了解:http://www.hmoore.net/manual/thinkphp6_0/1037489 | | $request | 請求對象 | 用于獲取請求的信息,更多了解:http://www.hmoore.net/manual/thinkphp6_0/1037518 | | $name | 插件標識名稱 | | | $addon_path | 當前插件根路徑 | | | $cache | 緩存 | 更多了解:http://www.hmoore.net/manual/thinkphp6_0/1037634 | | $site | 站點配置數組 | 即后臺站點配置,$this-site['title'] 獲取網站名稱 | **控制器方法** 1. `error`方法,輸出錯誤信息,如果是ajax請求則返回的是json數據。如果不是ajax確保視圖目錄下有error.html文件,你可通過$error_tmpl來指定位置,只能是視圖目錄下。 ~~~ // 在控制器里面使用 public function test() { $this->error('出錯了'); } // $msg 提示信息 // $url = 跳轉地址 // $data = 返回的數據 // $wait = 等待幾秒后跳轉,默認3秒 // $header = 頭部信息 $this->error($msg, $url, $data,$wait, $header) ~~~ 2. `success`方法,輸出正確提示信息,同上一樣。 3. `result` 方法返回指定的數據類型,例如json、xml,默認是json,常用于接口。 ~~~ public function test() { $this->result('操作成功',['lists'=>[]],200, 'json'); } ~~~ ~~~ /** * $msg 提示信息 * $data 要返回的數據 * $code 返回的code * $type 返回數據格式 * $header 發送的Header信息 */ $this->result($msg = '', $data = [], $code = 200, $type = 'json', array $header = []) ~~~ 4. `fetch`加載模板頁面方法 如下,參數一填寫模板位置,位置是基于插件視圖為基準,另外如果為空默認以當前控制器/操作方法方式找到模板位置。參數二為模板變量參數 ~~~ /** * $template 模板文件名 * $vars 模板輸出變量 */ $this->fetch($template = '', $vars = []) ~~~ 以demo插件為例,以下是Index控制器加載模板,參數沒有填寫那么加載的模板路徑為:addons/demo/view/index/test.html ~~~ public function test() { return $this->fetch(); } ~~~ 加載其他模板,路徑為:addons/demo/view/test/index.html ~~~ public function test() { return $this->fetch('test/index'); } ~~~ 5. `display`渲染模板內容 與fetch方法不同的是,fetch方法時獲取傳入的文件位置,display方法是直接傳入字符串渲染。 ~~~ /** * @param string $content 模板內容 * @param array $vars 模板輸出變量 */ $this->display($content = '', $vars = []) ~~~ 示例 ~~~ public function test() { $html = '<a>{$test}</a>'; return $this->display($html, ['test'=>111]); } ~~~ 6. `assign`模板變量賦值方法 ~~~ /** * @param mixed $name 要顯示的模板變量 * @param mixed $value 變量的值 */ $this->assign($name, $value = '') ~~~ 示例:如下,這樣定義好后,在模板頁面寫入{$txt}即可。 ~~~ public function test() { $txt = '測試'; $this->assign('txt', $txt); return $this->fetch(); } ~~~ 7. `getInfo`獲取插件信息 ~~~ public function test() { $ini = $this->getInfo(); dump($ini); } ~~~ 8. `getConfig`獲取插件配置 true = 獲取完整配置,即返回config.php里面的完整數組,false 僅獲取鍵值。 ``` /** * @param bool $type 是否獲取完整配置 */ $this->getConfig($type = false) ``` 示例 ~~~ public function test() { $config = $this->getConfig(); dump($config); } ~~~ 可參考內容: [插件內置函數](%E6%8F%92%E4%BB%B6%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0.md)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看