多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 16. 插件路由 ## 插件路由 默认情况下我们的插件前台访问地址是 [http://xxxx.com/addons/插件名称/控制器/操作方法](http://xxxx.com/addons/%E6%8F%92%E4%BB%B6%E5%90%8D%E7%A7%B0/%E6%8E%A7%E5%88%B6%E5%99%A8/%E6%93%8D%E4%BD%9C%E6%96%B9%E6%B3%95) 例如访问demo插件的Index控制器,index操作方法,那么地址如下: <http://xxxx.com/addons/demo/index/index> ## 自定义路由 我们可以通过自定义路由实现个性化插件访问,有自动自定义路由或手动自定义路由方式。 路由参考链接:[https://www.kancloud.cn/manual/thinkphp6\_0/1037495](https://www.kancloud.cn/manual/thinkphp6_0/1037495) > 注意:2.1.220220版本暂时只能手动写路由。 - 手动写路由 如下,定义好后浏览器访问:<http://xxxx.com/ceshi> ,就能去到demo插件下的Index控制器下的index操作方法 ``` <pre class="calibre16">``` <span class="token5">/** * 插件初始化 */</span> public <span class="token2">function</span> <span class="token6">addonsInitHook</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $execute <span class="token">=</span> <span class="token4">'\\think\\addons\\Route::execute'</span><span class="token1">;</span> <span class="token5">// url地址,即浏览器输入的地址</span> \think\facade\Route<span class="token1">:</span><span class="token1">:</span><span class="token6">rule</span><span class="token1">(</span><span class="token4">'/ceshi'</span><span class="token1">,</span> $execute<span class="token1">)</span> <span class="token">-</span><span class="token">></span><span class="token6">append</span><span class="token1">(</span><span class="token1">[</span> <span class="token4">'addon'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'demo'</span><span class="token1">,</span><span class="token5">// 插件名称</span> <span class="token4">'controller'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'index'</span><span class="token1">,</span><span class="token5">//控制器</span> <span class="token4">'action'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'index'</span> <span class="token5">//操作方法</span> <span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` ## 自动导入路由文件 在插件根目录下新建`route.php`文件,路径为:addons/插件名称/route.php 示例如下:定义好后浏览器访问:<http://xxxx.com/ceshi> ,就能去到demo插件下的Index控制器下的index操作方法。 ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php <span class="token2">return</span> <span class="token1">[</span> <span class="token5">// url地址=>插件名称/控制器/操作方法</span> <span class="token4">'ceshi'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/index/index'</span> <span class="token1">]</span><span class="token1">;</span> ``` ``` - **域名形式** 下面绑定了两个域名,那么访问地址是:[http://demo.xxxx.com/ceshi,以及](http://demo.xxxx.com/ceshi%EF%BC%8C%E4%BB%A5%E5%8F%8A)<http://local.xxx.com/ceshi> ``` <pre class="calibre16">``` <span class="token"><</span><span class="token">?</span>php <span class="token2">return</span> <span class="token1">[</span> <span class="token1">[</span> <span class="token4">'domain'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'demo'</span><span class="token1">,</span> <span class="token4">'rule'</span> <span class="token">=</span><span class="token">></span> <span class="token1">[</span> <span class="token4">'ceshi'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/index/index'</span> <span class="token1">]</span> <span class="token1">]</span><span class="token1">,</span> <span class="token1">[</span> <span class="token4">'domain'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'local'</span><span class="token1">,</span> <span class="token4">'rule'</span> <span class="token">=</span><span class="token">></span> <span class="token1">[</span> <span class="token4">'ceshi'</span><span class="token">=</span><span class="token">></span><span class="token4">'demo/index/index'</span> <span class="token1">]</span> <span class="token1">]</span> <span class="token1">]</span><span class="token1">;</span> ``` ``` > 若要实现动态,例如给到用户来配置路由,那么你在这个文件里面可以查询数据库,按如上数组格式返回即可