thinkphp 請求

請求對象由 think\Request 負責 只須要依賴注入便可php

請求對象

構造方法注入便可html

public function __construct(Request $request)
    {
        $this->request = $request;
    }

請求信息

<?php

namespace app\index\controller;

use think\exception\ValidateException;
use think\facade\Request;
class Index extends BaseController
{

    /**
     * 顯示資源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        return Request::url();
    }

    /**
     * 顯示建立資源表單頁.
     *
     * @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';
    }
}

輸入網址 http://localhost:8082/ 返回斜槓ajax

輸入變量

須要引入thinkphp

use think\facade\Request;

在中間件裏設置變量,而後使用has進行檢測。
或者能夠輸入參數的方式獲取變量apache

路由設置json

<?php

use think\facade\Route;

Route::rule('ming/:name', 'index/index');

控制器設置緩存

<?php

namespace app\index\controller;

use think\exception\ValidateException;
use think\facade\Request;
class Index extends BaseController
{

    /**
     * 顯示資源列表
     *
     * @return \think\Response
     */
    public function index($name)
    {
        return Request::param('name');
    }

    /**
     * 顯示建立資源表單頁.
     *
     * @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';
    }
}

訪問 url http://localhost:8082/index/ming/45
此時頁面輸出 45
當請求爲?的時候,使用get獲取服務器

一樣能夠使用助手函數獲取app

請求類型

添加 _method 進行假裝dom

頭信息

<?php

namespace app\index\controller;

use think\exception\ValidateException;
use think\facade\Request;
class Index extends BaseController
{

    /**
     * 顯示資源列表
     *
     * @return \think\Response
     */
    public function index($name)
    {
        return Request::header('user-agent');
    }

    /**
     * 顯示建立資源表單頁.
     *
     * @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';
    }
}

輸入 http://localhost:8082/index/ming/name
返回當前的頭信息

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/74.0.3729.169 Chrome/74.0.3729.169 Safari/537.36

僞靜態

配置文件修改僞靜態。。

服務器須要支持.htass文件

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 應用設置
// +----------------------------------------------------------------------

return [
    // PATHINFO變量名 用於兼容模式
    'var_pathinfo'          => 's',
    // 兼容PATH_INFO獲取
    'pathinfo_fetch'        => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
    // pathinfo分隔符
    'pathinfo_depr'         => '/',
    // HTTPS代理標識
    'https_agent_name'      => '',
    // URL僞靜態後綴
    'url_html_suffix'       => 'html',
    // URL普通方式參數 用於自動生成
    'url_common_param'      => true,
    // 是否開啓路由延遲解析
    'url_lazy_route'        => false,
    // 是否強制使用路由
    'url_route_must'        => false,
    // 合併路由規則
    'route_rule_merge'      => false,
    // 路由是否徹底匹配
    'route_complete_match'  => false,
    // 使用註解路由
    'route_annotation'      => false,
    // 是否開啓路由緩存
    'route_check_cache'     => false,
    // 路由緩存鏈接參數
    'route_cache_option'    => [],
    // 路由緩存Key
    'route_check_cache_key' => '',
    // 訪問控制器層名稱
    'controller_layer'      => 'controller',
    // 空控制器名
    'empty_controller'      => 'Error',
    // 是否使用控制器後綴
    'controller_suffix'     => false,
    // 默認的路由變量規則
    'default_route_pattern' => '[\w\.]+',
    // 域名根,如thinkphp.cn
    'url_domain_root'       => '',
    // 是否自動轉換URL中的控制器和操做名
    'url_convert'           => true,
    // 表單請求類型假裝變量
    'var_method'            => '_method',
    // 表單ajax假裝變量
    'var_ajax'              => '_ajax',
    // 表單pjax假裝變量
    'var_pjax'              => '_pjax',
    // 是否開啓請求緩存 true自動緩存 支持設置請求緩存規則
    'request_cache'         => false,
    // 請求緩存有效期
    'request_cache_expire'  => null,
    // 全局請求緩存排除規則
    'request_cache_except'  => [],
    // 默認控制器名
    'default_controller'    => 'Index',
    // 默認操做名
    'default_action'        => 'index',
    // 操做方法後綴
    'action_suffix'         => '',
    // 默認JSONP格式返回的處理方法
    'default_jsonp_handler' => 'jsonpReturn',
    // 默認JSONP處理方法
    'var_jsonp_handler'     => 'callback',
];

鏈接以下

http://localhost:8082/index.php/index/ming/name.html

此時訪問的等價於

http://localhost:8082/index.php/index/ming/name

參數綁定

這個鏈接

http://localhost:8082/index/ming/year/34/month/34

定義路由

<?php

use think\facade\Route;

Route::rule('ming/year/:year/month/<month>', 'index/index');

其中month爲可選

控制器以下

<?php

namespace app\index\controller;

use think\exception\ValidateException;
use think\facade\Request;
class Index extends BaseController
{

    /**
     * 顯示資源列表
     *
     * @return \think\Response
     */
    public function index($year, $month = '01')
    {
        return $year . $month;
    }

    /**
     * 顯示建立資源表單頁.
     *
     * @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';
    }
}

此時輸入

http://localhost:8082/index/ming/year/34/month/34

返回
3434

請求緩存

將會對請求進行緩存

相關文章
相關標籤/搜索