ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
一般来说,控制器(controller)主要负责请求的接受,调用相关模型(Model)处理,最后通过视图(view)输出到浏览器。所以,控制器不应该过多的介入业务逻辑处理。 ## 新建一个控制器 ~~~ //在admin应用下建立一个index控制器 php think make:controller admin@Index //如果是单应用模式,则: php think make:controller Blog //不生成资源操作方法 php think make:controller admin@Index --plain ~~~ 一般建议继承一个基础的控制器,方便扩展。系统默认提供了一个`app\BaseController`控制器类,为了扩展,你也可以自己建一个自己的控制器。 下面给出一个新建的控制器样例。 ~~~ <?php declare (strict_types = 1); namespace app\admin\controller; use app\BaseController; use think\facade\Config; use think\Request; class index extends BaseController { /** * 显示资源列表 * * @return \think\Response */ public function index() { $arr[] = Config::get('database'); print_r($arr); } ...... } ~~~ 如果需要传递参数,可以通过index方法。代码如下。 ~~~ /* 通过访问:http://localhost:8080/admin/index/index/id/999 或者 http://localhost:8080/admin/index/index?id=999 */ public function index($id) { $arr[] = request()->param('id'); //也可以通过 input() 方法获取更多的参数,包括$_GET //也可以直接使用 $id变量 print_r($arr); } ~~~ ## 获取输入变量 常用的方法有: 1、静态调用。适用于依赖注入无法使用的场合。 ~~~ <?php namespace app\index\controller; use think\facade\Request; //注意:不是 think\Request class Index { public function index() { return Request::param('name'); } } ~~~ 2、助手函数。为了简化调用,系统还提供了`request`助手函数,可以在任何地方调用当前请求对象。 ~~~ <?php namespace app\index\controller; class Index { public function index() { return request()->param('name'); } } ~~~ 3、构造方法注入。 **一般适用于没有继承系统的控制器类的情况。** ~~~ <?php namespace app\index\controller; use think\Request; class Index { /** * @var \think\Request Request实例 */ protected $request; /** * 构造方法 * @param Request $request Request对象 * @access public */ public function __construct(Request $request) { $this->request = $request; } public function index() { return $this->request->param('name'); } } ~~~ ~~~ //以下静态方法需要:use think\facade\Request; //获取当前请求的全部变量(不包括$_FILES),如果不存在,使用默认值 $arr[] = Request::param('name', 'default'); //获取部分变量,部分变量设置默认值 $arr[] = Request::param(['id', 'name' => 'default']); // 获取get变量 并且不进行任何过滤 即使设置了全局过滤 $arr[] = Request::get('name', '', null); // 只获取POST请求的id和name变量 $arr[] = Request::only(['id', 'name' => 'default'], 'post'); // 排除GET请求的id和name变量 $arr[] = Request::except(['id', 'name'], 'get'); //变量可以带类型转换符,d为整数 $arr[] = Request::post('name/d'); //获取当前请求的变量,包括路由变量 $arr[] = input('id'); //助手函数,无需use ... input('param.name'); // 获取单个参数,等效于input('name'); input('param.'); // 获取全部参数,等效于input(''); ~~~ ## 变量类型列表 | 方法 | 描述 | | --- | --- | | param | 获取当前请求的全部变量,除了$\_FILES | | get | 获取 $\_GET 变量 | | post | 获取 $\_POST 变量 | | put | 获取 PUT 变量 | | delete | 获取 DELETE 变量 | | session | 获取 $\_SESSION 变量 | | cookie | 获取 $\_COOKIE 变量 | | request | 获取 $\_REQUEST 变量 | | server | 获取 $\_SERVER 变量 | | env | 获取 $\_ENV 变量 | | route | 获取 路由(包括PATHINFO) 变量 | | middleware | 获取 中间件赋值/传递的变量 | | file | 获取 $\_FILES 变量 | | all`V6.0.8+` | 获取包括 $\_FILES 变量在内的请求变量,相当于param+file | ## 获取请求类型列表 ~~~ if(request()->isAjax()){ ...... } ~~~ 请求对象`Request`类提供了下列方法来获取或判断当前请求类型: | 用途 | 方法 | | --- | --- | | 获取当前请求类型 | method | | 判断是否GET请求 | isGet | | 判断是否POST请求 | isPost | | 判断是否PUT请求 | isPut | | 判断是否DELETE请求 | isDelete | | 判断是否AJAX请求 | isAjax | | 判断是否PJAX请求 | isPjax | | 判断是否JSON请求 | isJson | | 判断是否手机访问 | isMobile | | 判断是否HEAD请求 | isHead | | 判断是否PATCH请求 | isPatch | | 判断是否OPTIONS请求 | isOptions | | 判断是否为CLI执行 | isCli | | 判断是否为CGI模式 | isCgi | ## 获取`app`下所有的方法 ~~~ /* 获取当前 app 下所有的方法 */ function getMethods() { /* app/admin/controller/*.php */ $files = glob(\think\facade\App::getAppPath() . 'controller/*'); /* tp 根目录 */ $root = \think\facade\App::getRootPath(); $m = []; /* 遍历 controller 下所有文件 */ foreach ($files as $index => $file) { $class = str_replace($root, '', $file); $class = strtr($class, '/', '\\'); $class = substr($class, 0, -4); $shortClass = ltrim(strrchr($class, '\\'), '\\'); $contents = file_get_contents($file); $needle = "class {$shortClass}"; $pos = stripos($contents, $needle); if ($pos === false) { continue; } $reflection = new \ReflectionClass($class); $methods = $reflection->getMethods(); foreach ($methods as $method) { $methodName = $method->getName(); /* 获取注释 */ $comment = $method->getDocComment(); if ($comment !== false) { preg_match("~[\s\*]+(.*)~im", $comment, $arr); $comment = $arr[1] ?: ''; } /* 只取public方法 */ if ($method->isPublic() && !\think\helper\Str::startsWith($methodName, '_')) { $m[$index][] = [$shortClass . '/' . $methodName, $comment]; } } } return $m; } ~~~