前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >thikphp 控制器

thikphp 控制器

原创
作者头像
mySoul
发布2019-05-30 17:40:01
6020
发布2019-05-30 17:40:01
举报
文章被收录于专栏:mySoul

控制器定义

类名和文件名一样,

渲染输出

渲染输出使用return输出

代码语言:txt
复制
<?php
namespace app\admin\controller;
use app\admin\model\User;

class Index
{

    public function Index(){
        $data = array(
            'ming' => 'ming',
            'ming' => 'xiao'
        );
        return json($data);
    }

}

此时页面渲染出json文件

2019-05-25-22-42-10----
2019-05-25-22-42-10----

不能在控制器中中断代码。。

使用halt输出

代码语言:txt
复制
<?php
namespace app\admin\controller;
use app\admin\model\User;

class Index
{

    public function Index(){
        $data = array(
            'ming' => 'ming',
            'ming' => 'xiao'
        );
        halt("输出测试");
        return json($data);
    }

}

使用halt 输出

2019-05-25-22-51-14----
2019-05-25-22-51-14----

多级控制器

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

代码语言:txt
复制
<?php


namespace 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

目录结构

2019-05-25-23-15-28----
2019-05-25-23-15-28----

定义路由规则

代码语言:txt
复制
<?php
use think\facade\Route;

Route::rule('blog/:id', 'index.blog/read');
Route::rule('/', 'Index/index');

访问index路由下的blog目录

基础控制器

控制器都会有一个基础控制器

系统会提供一个

代码语言:txt
复制
app\BaseController

基础控制器

目录文件如下

2019-05-25-23-51-06----
2019-05-25-23-51-06----

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

app\BaseController

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

2019-05-25-23-55-13----
2019-05-25-23-55-13----

更改命名空间

代码语言:txt
复制
namespace app\index\controller;

use think\App;
use think\exception\ValidateException;
use think\Validate;
代码语言:txt
复制
<?php

namespace 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)
    {
        //
    }
}

输出内容

代码语言:txt
复制
string(5) "index" string(43) "/home/ming/PhpstormProjects/untitled12/app/"

控制器验证

代码语言:txt
复制
<?php

namespace 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)
    {
        //
    }
}

这样控制器验证

空控制器

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

代码语言:txt
复制
    public function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
        return 'error request';
    }

资源控制器

创建restful控制器

输入

代码语言:txt
复制
php think make:controller index@Blog

生成资源控制器

生成api

代码语言:txt
复制
<?php

namespace 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)
    {
        //
    }
}

注册资源路由即可

代码语言:txt
复制
Route::resource('blog', 'Blog');

控制器中间件

编写控制器

代码语言:txt
复制
<?php


namespace app\index\middleware;

class Hello
{
    public function handle($request, \Closure $next){
        $request->hello = 'ming';
        return $next($request);
    }
}

使用路由注册控制器

代码语言:txt
复制
<?php

use think\facade\Route;

Route::rule('ming', 'index/index')->middleware(
    [
        app\index\middleware\Hello::class
    ]
);

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

出现 ming

说明中间件注册成功

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 控制器定义
  • 渲染输出
  • 多级控制器
  • 基础控制器
    • 控制器验证
    • 空控制器
    • 资源控制器
    • 控制器中间件
    相关产品与服务
    消息队列 TDMQ
    消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档