ThinkPHP5 控制器和视图路径关系简单示例
2023-03-29 23:03:03
316
{{single.collect_count}}

在ThinkPHP5的项目版本中,假设我们需要建立一个网站的二级目录 user

那么我们首先在application目录下新建一个名为User的文件夹,
然后在User下面新建一个名为controller的文件夹,
然后在controller下面新建index.php文件,代码如下:
 

<?php namespace app\User\controller;class index{public function index(){return "hello user";}}?>

此时,我们访问 : 域名/User/
效果如下:

这显示的是 控制器函数中 return 的字符串,如果我要使用视图文件:
1、在User目录先新建名为view的目录
2、在view目录下新建名为Index的目录
3、在Index目录下新建 index.html

暂时把index.html内容编辑为

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>User's Name</title></head><body>Hello,User,This is your view!</body></html>

然后,我们修改刚才的index.php为:
 

<?php namespace app\User\controller;class Index{public function index(){return view(); //修改了这一句,输出对应视图}}?>

然后,我们刷新页面 域名/User/
效果为:

总结一下:

域名/User/ 这个路径,对应application目录下的User文件夹
控制器文件:application/User/controller/Index.php
视图的文件:application/User/view/Index/index.html

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}