<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                一般來說,控制器(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; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看