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文件
不能在控制器中中断代码。。
使用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 输出
多级控制器多级控制器 多级控制器直接在命名空间中使用
<?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
目录结构
定义路由规则
<?phpuse think\facade\route;route::rule('blog/:id', 'index.blog/read');route::rule('/', 'index/index');
访问index路由下的blog目录
基础控制器控制器都会有一个基础控制器
系统会提供一个
app\basecontroller
基础控制器
目录文件如下
所有的控制都有一个基础控制类
appbasecontroller
由于是多应用模式。。基础类移动到目录下
更改命名空间
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) { // } /** * 显示指定的资源 * * @param int $id * @return \think\response */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return \think\response */ public function edit($id) { // } /** * 保存更新的资源 * * @param \think\request $request * @param int $id * @return \think\response */ public function update(request $request, $id) { // } /** * 删除指定资源 * * @param int $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) { // } /** * 显示指定的资源 * * @param int $id * @return \think\response */ public function read($id) { // } /** * 显示编辑资源表单页. * * @param int $id * @return \think\response */ public function edit($id) { // } /** * 保存更新的资源 * * @param \think\request $request * @param int $id * @return \think\response */ public function update(request $request, $id) { // } /** * 删除指定资源 * * @param int $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) { // } /** * 显示指定的资源 * * @param int $id * @return \think\response */ public function read($id) { // } /** * 保存更新的资源 * * @param \think\request $request * @param int $id * @return \think\response */ public function update(request $request, $id) { // } /** * 删除指定资源 * * @param int $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
说明中间件注册成功。
推荐:《最新的10个thinkphp视频教程》
以上就是一文详解thinkphp控制器的定义和使用的详细内容。
