企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 9. 插件控制器 ## 插件控制器 插件除了基于前台与后台外,还可以作为独立的模块。创建控制器后即可赋予插件前台访问功能。 ## 创建控制器 控制器放在`controller`文件夹下,以`demo`插件为例,创建首页控制器,路径为:addons/demo/controller/Index.php,如下图 ![](https://img.kancloud.cn/8c/c7/8cc77f51bb2a94ca11905d43e3272c7c_308x128.png) Index.php 代码 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php namespace addons\demo\controller<span class="token1">;</span> use think\addons\Controller<span class="token1">;</span> class <span class="token6">Index</span> extends <span class="token6">Controller</span> <span class="token1">{</span> public <span class="token2">function</span> <span class="token6">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'首页'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` 浏览器访问地址格式:[http://xxx.com/addons/插件名称/控制器名/操作方法](http://xxx.com/addons/%E6%8F%92%E4%BB%B6%E5%90%8D%E7%A7%B0/%E6%8E%A7%E5%88%B6%E5%99%A8%E5%90%8D/%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95) 如上`demo`插件,那么首页控制器访问如下,没写控制器那么访问的是`Index`控制器,操作方法默认是`index` <http://xxx.com/addons/demo/index/index> 首页默认是Index,操作方法index,那么可以省略,如下即可直接访问到 <http://xxx.com/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容器变量有路由、配置、路由等实例对象,更多了解:[https://www.kancloud.cn/manual/thinkphp6\_0/1037489](https://www.kancloud.cn/manual/thinkphp6_0/1037489)$request请求对象用于获取请求的信息,更多了解:[https://www.kancloud.cn/manual/thinkphp6\_0/1037518](https://www.kancloud.cn/manual/thinkphp6_0/1037518)$name插件标识名称$addon\_path当前插件根路径$cache缓存更多了解:[https://www.kancloud.cn/manual/thinkphp6\_0/1037634](https://www.kancloud.cn/manual/thinkphp6_0/1037634)$site站点配置数组即后台站点配置,$this-site\['title'\] 获取网站名称**控制器方法** 1. `error`方法,输出错误信息,如果是ajax请求则返回的是json数据。如果不是ajax确保视图目录下有error.html文件,你可通过$error\_tmpl来指定位置,只能是视图目录下。 ``` <pre class="calibre16">``` <span class="token5">// 在控制器里面使用</span> public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $this<span class="token">-</span><span class="token">></span><span class="token6">error</span><span class="token1">(</span><span class="token4">'出错了'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">// $msg 提示信息</span> <span class="token5">// $url = 跳转地址</span> <span class="token5">// $data = 返回的数据</span> <span class="token5">// $wait = 等待几秒后跳转,默认3秒</span> <span class="token5">// $header = 头部信息</span> $this<span class="token">-</span><span class="token">></span><span class="token6">error</span><span class="token1">(</span>$msg<span class="token1">,</span> $url<span class="token1">,</span> $data<span class="token1">,</span>$wait<span class="token1">,</span> $header<span class="token1">)</span> ``` ``` 2. `success`方法,输出正确提示信息,同上一样。 3. `result` 方法返回指定的数据类型,例如json、xml,默认是json,常用于接口。 ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $this<span class="token">-</span><span class="token">></span><span class="token6">result</span><span class="token1">(</span><span class="token4">'操作成功'</span><span class="token1">,</span><span class="token1">[</span><span class="token4">'lists'</span><span class="token">=</span><span class="token">></span><span class="token1">[</span><span class="token1">]</span><span class="token1">]</span><span class="token1">,</span><span class="token3">200</span><span class="token1">,</span> <span class="token4">'json'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` ``` <pre class="calibre16">``` <span class="token5">/** * $msg 提示信息 * $data 要返回的数据 * $code 返回的code * $type 返回数据格式 * $header 发送的Header信息 */</span> $this<span class="token">-</span><span class="token">></span><span class="token6">result</span><span class="token1">(</span>$msg <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $data <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">,</span> $code <span class="token">=</span> <span class="token3">200</span><span class="token1">,</span> $type <span class="token">=</span> <span class="token4">'json'</span><span class="token1">,</span> array $header <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">)</span> ``` ``` 4. `fetch`加载模板页面方法 如下,参数一填写模板位置,位置是基于插件视图为基准,另外如果为空默认以当前控制器/操作方法方式找到模板位置。参数二为模板变量参数 ``` <pre class="calibre16">``` <span class="token5">/** * $template 模板文件名 * $vars 模板输出变量 */</span> $this<span class="token">-</span><span class="token">></span><span class="token6">fetch</span><span class="token1">(</span>$template <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $vars <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">)</span> ``` ``` 以demo插件为例,以下是Index控制器加载模板,参数没有填写那么加载的模板路径为:addons/demo/view/index/test.html ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> $this<span class="token">-</span><span class="token">></span><span class="token6">fetch</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 加载其他模板,路径为:addons/demo/view/test/index.html ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> $this<span class="token">-</span><span class="token">></span><span class="token6">fetch</span><span class="token1">(</span><span class="token4">'test/index'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 5. `display`渲染模板内容 与fetch方法不同的是,fetch方法时获取传入的文件位置,display方法是直接传入字符串渲染。 ``` <pre class="calibre16">``` <span class="token5">/** * @param string $content 模板内容 * @param array $vars 模板输出变量 */</span> $this<span class="token">-</span><span class="token">></span><span class="token6">display</span><span class="token1">(</span>$content <span class="token">=</span> <span class="token4">''</span><span class="token1">,</span> $vars <span class="token">=</span> <span class="token1">[</span><span class="token1">]</span><span class="token1">)</span> ``` ``` 示例 ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $html <span class="token">=</span> <span class="token4">'<a>{$test}</a>'</span><span class="token1">;</span> <span class="token2">return</span> $this<span class="token">-</span><span class="token">></span><span class="token6">display</span><span class="token1">(</span>$html<span class="token1">,</span> <span class="token1">[</span><span class="token4">'test'</span><span class="token">=</span><span class="token">></span><span class="token3">111</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 6. `assign`模板变量赋值方法 ``` <pre class="calibre16">``` <span class="token5">/** * @param mixed $name 要显示的模板变量 * @param mixed $value 变量的值 */</span> $this<span class="token">-</span><span class="token">></span><span class="token6">assign</span><span class="token1">(</span>$name<span class="token1">,</span> $value <span class="token">=</span> <span class="token4">''</span><span class="token1">)</span> ``` ``` 示例:如下,这样定义好后,在模板页面写入{$txt}即可。 ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $txt <span class="token">=</span> <span class="token4">'测试'</span><span class="token1">;</span> $this<span class="token">-</span><span class="token">></span><span class="token6">assign</span><span class="token1">(</span><span class="token4">'txt'</span><span class="token1">,</span> $txt<span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> $this<span class="token">-</span><span class="token">></span><span class="token6">fetch</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 7. `getInfo`获取插件信息 ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $ini <span class="token">=</span> $this<span class="token">-</span><span class="token">></span><span class="token6">getInfo</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">dump</span><span class="token1">(</span>$ini<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 8. `getConfig`获取插件配置 true = 获取完整配置,即返回config.php里面的完整数组,false 仅获取键值。 ``` <pre class="calibre16">``` <span class="token5">/** * @param bool $type 是否获取完整配置 */</span> $this<span class="token">-</span><span class="token">></span><span class="token6">getConfig</span><span class="token1">(</span>$type <span class="token">=</span> <span class="token3">false</span><span class="token1">)</span> ``` ``` 示例 ``` <pre class="calibre16">``` public <span class="token2">function</span> <span class="token6">test</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $config <span class="token">=</span> $this<span class="token">-</span><span class="token">></span><span class="token6">getConfig</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">dump</span><span class="token1">(</span>$config<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 可参考内容: [插件内置函数](Cha%20Jian%20Nei%20Zhi%20Han%20Shu.html)