初用 TP5 框架, TP5 框架能不能实现模块里有子模块这种操作?

2020-07-27 11:27:12 +08:00
 5bb864e1fc775087

现有这个文件

\tp\application\index\controller\Controller1.php

用这个 url 可以正常访问, 不报错

http://tp/index/controller1/func1


想实现文件这样存放, 多一级子目录(子模块)

\tp\application\index\module1\controller\Controller1.php

但是用这个 url 访问会报错

http://tp/index/module1/controller1/func1

报错信息

控制器不存在:app\index\controller\Module1
1541 次点击
所在节点    问与答
19 条回复
5bb864e1fc775087
2020-07-27 11:29:00 +08:00
能不能做到 以及 要怎么做才行
qiayue
2020-07-27 11:35:09 +08:00
是不是少了一级目录
http://tp/index/module1/controller/controller1/func1
5bb864e1fc775087
2020-07-27 11:36:33 +08:00
@qiayue #2 你这个 URL 打开报一样的错误"控制器不存在:app\index\controller\Module1",URL 中间的“controller”,TP 框架会自动补充
5bb864e1fc775087
2020-07-27 12:34:21 +08:00
没人回复,这个问题无解么?
loongk
2020-07-27 12:40:58 +08:00
http://tp/index/module1.controller1/func1
fengshils
2020-07-27 12:46:23 +08:00
推荐使用命令行创建子模块控制器,盲猜是命名空间没写对
5bb864e1fc775087
2020-07-27 12:50:24 +08:00
@loongk #5 报错“控制器不存在:app\index\controller\module1\Controller1”
5bb864e1fc775087
2020-07-27 12:54:17 +08:00
@fengshils #6 命名空间我有专门检查过,还是一样。我觉得主要是因为 TP 框架会自动在类名 app\index 后面加上"controller",导致类名成了 app\index\controller\module1\controller\Controller1 但是正确的类名是 app\index\module1\controller\Controller1
fengshils
2020-07-27 13:39:18 +08:00
试试
```
php think make:controller index/Controller/Module1/Controller1
```
5bb864e1fc775087
2020-07-27 13:50:35 +08:00
执行后会生成这个文件
\tp\application\index\controller\Controller\Module1\Controller1.php
然后用这种方式访问到控制器
http://tp/index/controller.Module1.Controller1/index

但和我要的不一样,我要的是这样
\tp\application\index\module1\controller\Controller1.php
最后没办法的话,我也只能这样用了,把 module1 子模块的文件放在“\tp\application\index\controller”里面用了
fengshils
2020-07-27 14:09:06 +08:00
.的话好像不支持把,估计你得改源代码了
back0893
2020-07-27 14:31:17 +08:00
直接写路由不就好了
doublie
2020-07-27 14:31:50 +08:00
Route::get('index/module1/controller1/func1', 'index/module1.Controller1/func1');
这样配置一下路由不知道行不行
5bb864e1fc775087
2020-07-27 14:59:01 +08:00
@doublie #13
@back0893 #12
写路由我也有想到,就是每个接口都要写,有点多
cnoder
2020-07-27 17:40:11 +08:00
路由也不是每个接口都要写,可以只给这个子模块的配置路由
absolutelynull
2020-07-27 17:53:43 +08:00
\tp\application\index\controller\module1\Controller1.php

index.module1/controller1
5bb864e1fc775087
2020-07-27 18:31:11 +08:00
@absolutelynull #16
本来我想要的是 \tp\application\index\module1,目测是没法实现了
只能用跟你说的这种把子模块放在 index\controller 里的 \tp\application\index\controller\module1 方式了
JaguarJack
2020-07-27 22:14:31 +08:00
没办法的 你看源码就会发现的 写死的规则
absolutelynull
2020-07-29 09:10:19 +08:00
@5bb864e1fc775087 url 访问控制器的话 只能访问到 controller 下面的目录文件
不然的话 就不会出现 controller model view 这几个目录了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/693395

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX