thinkphp-请求

12次阅读

共计 6258 个字符,预计需要花费 16 分钟才能阅读完成。

请求对象由 thinkRequest 负责 只需要依赖注入即可

请求对象

构造方法注入即可

 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/ 返回斜杠

输入变量

需要引入

use think\facade\Request;

在中间件里设置变量,然后使用 has 进行检测。
或者可以输入参数的方式获取变量

路由设置

<?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 获取

同样可以使用助手函数获取

请求类型

添加 _method 进行伪装

头信息

<?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

请求缓存

将会对请求进行缓存

正文完
 0