一般來說,控制器(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;
}
~~~
- 搭建ThinkPHP6的開發環境
- 配置ThinkPHP6
- 必要的基礎知識(basic)
- MVC開發模式
- 控制器(controller)
- 數據庫(database)
- 模型(model)
- 模型關聯(relation)
- 視圖(view)
- Session
- Cookie
- 緩存(cache)
- 上傳(upload)
- 驗證器(validate)
- 驗證碼(captcha)
- 命令行(command)
- 服務器部署(deploy)
- 數據備份(backup)
- 數據同步(synchronization)
- 訂閱服務(subscribe)
- PHP 易混淆知識點
- 助手函數
- MySQL規范
- Redis 規范
- office插件 phpoffice
- 拼音插件 pinyin
- 日期插件 datetime
- 消息插件 amqp
- 產品部署環境的搭建
- PDF 等雜項處理
- 文件上傳
- 常用擴展
- flc/dysms
- 使用示例 ①
- 使用示例 ②
- qiniu/php-sdk
- 簡介
- 使用示例
- 使用示例 2 ②
- liliuwei/thinkphp-jump
- 擴展介紹
- 下載擴展
- 使用方法
- topthink/think-captcha
- 安裝擴展
- 驗證碼顯示
- 更換驗證碼
- 驗證碼校驗
- 驗證碼配置
- 自定義驗證碼
- phpoffice/phpspreadsheet
- 數據寫入表格
- 讀取表格數據
- topthink/think-queue
- 安裝
- 自定義函數
- 任務類
- 帶有日志的任務類