一文详解thinkphp控制器的定义和使用
2021-08-23 15:30:08
下面由thinkphp框架教程栏目给大家介绍thinkphp控制器的定义和使用,希望对需要的朋友有所帮助!
控制器定义
类名和文件名一样,
渲染输出
渲染输出使用return输出
<?php namespace appadmincontroller; use appadminmodelUser; class Index { public function Index(){ $data = array( 'ming' => 'ming', 'ming' => 'xiao' ); return json($data); } }
此时页面渲染出json文件
不能在控制器中中断代码。。
使用halt输出
<?php namespace appadmincontroller; use appadminmodelUser; class Index { public function Index(){ $data = array( 'ming' => 'ming', 'ming' => 'xiao' ); halt("输出测试"); return json($data); } }
使用halt 输出
多级控制器
多级控制器 多级控制器直接在命名空间中使用
<?php namespace appadmincontrollerIndex; class Blog { public function index(){ } public function read($id){ var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming'])); return $id; } }
定义了Index命名空间下的子控制器 Blog
目录结构
定义路由规则
<?php use thinkfacadeRoute; Route::rule('blog/:id', 'index.blog/read'); Route::rule('/', 'Index/index');
访问index路由下的blog目录
基础控制器
控制器都会有一个基础控制器
系统会提供一个
appBaseController
基础控制器
目录文件如下
所有的控制都有一个基础控制类
appBaseController
由于是多应用模式。。基础类移动到目录下
更改命名空间
namespace appindexcontroller; use thinkApp; use thinkexceptionValidateException; use thinkValidate;
<?php namespace appindexcontroller; use thinkRequest; class Index extends BaseController { /** * 显示资源列表 * * @return thinkResponse */ public function index() { $action = $this->request->action(); $path = $this->app->getBasePath(); var_dump($action); var_dump($path); } /** * 显示创建资源表单页. * * @return thinkResponse */ public function create() { // } /** * 保存新建的资源 * * @param thinkRequest $request * @return thinkResponse */ public function save(Request $request) { // } /** * 显示指定的资源 * * @param int $id * @return thinkResponse */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return thinkResponse */ public function edit($id) { // } /** * 保存更新的资源 * * @param thinkRequest $request * @param int $id * @return thinkResponse */ public function update(Request $request, $id) { // } /** * 删除指定资源 * * @param int $id * @return thinkResponse */ public function delete($id) { // } }
输出内容
string(5) "index" string(43) "/home/ming/PhpstormProjects/untitled12/app/"
控制器验证
<?php namespace appindexcontroller; use thinkexceptionValidateException; use thinkRequest; class Index extends BaseController { /** * 显示资源列表 * * @return thinkResponse */ public function index() { try { $this->validate( [ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com', ], 'appindexvalidateUser'); } catch (ValidateException $e) { // 验证失败 输出错误信息 dump($e->getError()); } } /** * 显示创建资源表单页. * * @return thinkResponse */ public function create() { // } /** * 保存新建的资源 * * @param thinkRequest $request * @return thinkResponse */ public function save(Request $request) { // } /** * 显示指定的资源 * * @param int $id * @return thinkResponse */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return thinkResponse */ public function edit($id) { // } /** * 保存更新的资源 * * @param thinkRequest $request * @param int $id * @return thinkResponse */ public function update(Request $request, $id) { // } /** * 删除指定资源 * * @param int $id * @return thinkResponse */ public function delete($id) { // } }
这样控制器验证
空控制器
空控制器是当找不到的方法的时候调用的方法
public function __call($name, $arguments) { // TODO: Implement __call() method. return 'error request'; }
资源控制器
创建restful控制器
输入
php think make:controller index@Blog
生成资源控制器
生成api
<?php namespace appindexcontroller; use thinkRequest; class Blog { /** * 显示资源列表 * * @return thinkResponse */ public function index() { // } /** * 保存新建的资源 * * @param thinkRequest $request * @return thinkResponse */ public function save(Request $request) { // } /** * 显示指定的资源 * * @param int $id * @return thinkResponse */ public function read($id) { // } /** * 保存更新的资源 * * @param thinkRequest $request * @param int $id * @return thinkResponse */ public function update(Request $request, $id) { // } /** * 删除指定资源 * * @param int $id * @return thinkResponse */ public function delete($id) { // } }
注册资源路由即可
Route::resource('blog', 'Blog');
控制器中间件
编写控制器
<?php namespace appindexmiddleware; class Hello { public function handle($request, Closure $next){ $request->hello = 'ming'; return $next($request); } }
使用路由注册控制器
<?php use thinkfacadeRoute; Route::rule('ming', 'index/index')->middleware( [ appindexmiddlewareHello::class ] );
访问 http://localhost:8082/index/ming
出现 ming
说明中间件注册成功。
推荐:《最新的10个thinkphp视频教程》
以上就是一文详解thinkphp控制器的定义和使用的详细内容,更多请关注dnjidi.com其它相关文章!
相关阅读
- thinkphp5与laravel的区别是什么
- Thinkphp+layer+ajax如何完成增加方法(附代码示例)
- 解析yylAdmin系统的安装使用
- 详解ThinkPHP怎么实现图片上传
- 有关ThinkAPI短信服务的详细介绍
- ThinkPHP有三种方式收集表单数据
- 一文详解thinkphp控制器的定义和使用
- 分享TP6框架中Redis操作服务类的记录
- 详解Tp5中如何定义获取器和修改器
- php onethink验证码不显示怎么办
- 解决TP获取微信用户信息出现10003错误问题
- TP框架去掉home模块的两种方法
- ThinkPHP怎么配置路由,让后台隐藏起来!
- 教你如何使用TP5 taglib自定义标签
- 解决TP5取不到Model的自定义字段问题
- 一文讲解thinkphp5如何进行异步验证邮箱
- 手把手教你使用ThinkPHP+phpExcel导入导出Excel数据(实践)
- 解析TP5框架从入口到输出界面的加载流程