一文详解thinkphp控制器的定义和使用
2022-11-24 11:02:51
197
{{single.collect_count}}
下面由thinkphp框架教程栏目给大家介绍thinkphp控制器的定义和使用,希望对需要的朋友有所帮助!

控制器定义

类名和文件名一样,

渲染输出

渲染输出使用return输出

<?phpnamespace app\admin\controller;use app\admin\model\User;class Index{public function Index(){$data = array('ming' => 'ming','ming' => 'xiao');return json($data);}}
登录后复制

此时页面渲染出json文件
0b682a98fe1e8a4ef2f8bcb96a98fe7.png

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

不能在控制器中中断代码。。
使用halt输出

<?phpnamespace app\admin\controller;use app\admin\model\User;class Index{public function Index(){$data = array('ming' => 'ming','ming' => 'xiao');halt("输出测试");return json($data);}}
登录后复制

使用halt 输出

f76595e6270890328c7d705da40d15f.png

多级控制器

多级控制器 多级控制器直接在命名空间中使用

<?phpnamespace app\admin\controller\Index;class Blog{public function index(){}public function read($id){var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming']));return $id;}}
登录后复制

定义了Index命名空间下的子控制器 Blog
目录结构
ad3ee79ba52b5ff66ae7762958c9e83.png

定义路由规则

<?phpuse think\facade\Route;Route::rule('blog/:id', 'index.blog/read');Route::rule('/', 'Index/index');
登录后复制

访问index路由下的blog目录

基础控制器

控制器都会有一个基础控制器
系统会提供一个

app\BaseController
登录后复制

基础控制器

目录文件如下
62c9d0fab66a68e23ed482a4ddac881.png

所有的控制都有一个基础控制类
appBaseController

由于是多应用模式。。基础类移动到目录下
bd6966045fda5e5ad4ff8c541df39ff.png

更改命名空间

namespace app\index\controller;use think\App;use think\exception\ValidateException;use think\Validate;
登录后复制
<?phpnamespace app\index\controller;use think\Request;class Index extends BaseController{/** * 显示资源列表 * * @return \think\Response */public function index(){$action = $this->request->action();$path = $this->app->getBasePath();var_dump($action);var_dump($path);}/** * 显示创建资源表单页. * * @return \think\Response */public function create(){//}/** * 保存新建的资源 * * @param\think\Request$request * @return \think\Response */public function save(Request $request){//}/** * 显示指定的资源 * * @paramint$id * @return \think\Response */public function read($id){//}/** * 显示编辑资源表单页. * * @paramint$id * @return \think\Response */public function edit($id){//}/** * 保存更新的资源 * * @param\think\Request$request * @paramint$id * @return \think\Response */public function update(Request $request, $id){//}/** * 删除指定资源 * * @paramint$id * @return \think\Response */public function delete($id){//}}
登录后复制

输出内容

string(5) "index" string(43) "/home/ming/PhpstormProjects/untitled12/app/"
登录后复制

控制器验证

<?phpnamespace app\index\controller;use think\exception\ValidateException;use think\Request;class Index extends BaseController{/** * 显示资源列表 * * @return \think\Response */public function index(){try {$this->validate( ['name'=> 'thinkphp','email' => 'thinkphp@qq.com',],'app\index\validate\User');} catch (ValidateException $e) {// 验证失败 输出错误信息dump($e->getError());}}/** * 显示创建资源表单页. * * @return \think\Response */public function create(){//}/** * 保存新建的资源 * * @param\think\Request$request * @return \think\Response */public function save(Request $request){//}/** * 显示指定的资源 * * @paramint$id * @return \think\Response */public function read($id){//}/** * 显示编辑资源表单页. * * @paramint$id * @return \think\Response */public function edit($id){//}/** * 保存更新的资源 * * @param\think\Request$request * @paramint$id * @return \think\Response */public function update(Request $request, $id){//}/** * 删除指定资源 * * @paramint$id * @return \think\Response */public function delete($id){//}}
登录后复制

这样控制器验证

空控制器

空控制器是当找不到的方法的时候调用的方法

public function __call($name, $arguments){// TODO: Implement __call() method.return 'error request';}
登录后复制

资源控制器

创建restful控制器
输入

php think make:controller index@Blog
登录后复制

生成资源控制器
生成api

<?phpnamespace app\index\controller;use think\Request;class Blog{/** * 显示资源列表 * * @return \think\Response */public function index(){//}/** * 保存新建的资源 * * @param\think\Request$request * @return \think\Response */public function save(Request $request){//}/** * 显示指定的资源 * * @paramint$id * @return \think\Response */public function read($id){//}/** * 保存更新的资源 * * @param\think\Request$request * @paramint$id * @return \think\Response */public function update(Request $request, $id){//}/** * 删除指定资源 * * @paramint$id * @return \think\Response */public function delete($id){//}}
登录后复制

注册资源路由即可

Route::resource('blog', 'Blog');
登录后复制

控制器中间件

编写控制器

<?phpnamespace app\index\middleware;class Hello{public function handle($request, \Closure $next){$request->hello = 'ming';return $next($request);}}
登录后复制

使用路由注册控制器

<?phpuse think\facade\Route;Route::rule('ming', 'index/index')->middleware([app\index\middleware\Hello::class]);
登录后复制

访问 http://localhost:8082/index/ming
出现 ming

说明中间件注册成功。

以上就是一文详解thinkphp控制器的定义和使用的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{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 ? '加载中...' : '查看更多评论'}}