关于 Laravel Requset 的问题

关于Laravel  Requset的问题
用offsetUnset去掉这个字段之后,打印$request->all() 依旧有 password字段。
理想效果是调用all方法能过滤掉自己想过滤的字段。

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

如果改成这样就对了

  /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        $this->getInputSource()->remove($offset);
        $this->query->remove($offset);
    }
4年前 评论
讨论数量: 11

$request->only(['字段1','字段2'])

4年前 评论
striver (楼主) 4年前
monanxiao (作者) 4年前
lochpure
$request->except(['password']);

这样吗?剔除不需要的字段

4年前 评论
striver (楼主) 4年前
lochpure (作者) 4年前
EmptyCup 4年前

,,,你为啥一定要通过这个方式去掉这个值,,,

不过你可以看看 $request->all() 的源码,看他从哪里搞来的所有数据,,,然后对症下药

4年前 评论

我试了下。unset后不会有这样的情况,看了下源码

    /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        $this->getInputSource()->remove($offset);
    }
    /**
     * Get all of the input and files for the request.
     *
     * @param  array|mixed|null  $keys
     * @return array
     */
    public function all($keys = null)
    {
        $input = array_replace_recursive($this->input(), $this->allFiles());

        if (! $keys) {
            return $input;
        }

        $results = [];

        foreach (is_array($keys) ? $keys : func_get_args() as $key) {
            Arr::set($results, $key, Arr::get($input, $key));
        }

        return $results;
    }

我觉得是删掉了的。。因为从inputSource删了。

4年前 评论
striver (楼主) 4年前
sunrise丶 (作者) 4年前

之所以想剔除掉all里面的参数,是想从全局过滤用户参数,已达到不用一直传参数。 file

file

file

file

4年前 评论
DotO 4年前
striver (作者) (楼主) 4年前

今早发现。post和put方法就能过滤掉。不过只能过滤掉body里面的参数,是query里面传过来的就过滤不掉,不知道算不算bug。

4年前 评论

是的,应该是个小bug。
如果是POST,这时候offsetUnset,不会去删除查询字符串里面的参数。

file

4年前 评论

如果改成这样就对了

  /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        $this->getInputSource()->remove($offset);
        $this->query->remove($offset);
    }
4年前 评论

@di-gua @striver 不是 bug 哟,你们思路错了

$request->request->remove('status');
4年前 评论
JerryBool 4年前
JerryBool 4年前
王成涛 (作者) 4年前
JerryBool 4年前

file

file

4年前 评论
JerryBool 4年前
striver (楼主) 4年前
王成涛 (作者) 4年前
JerryBool 4年前
JerryBool 4年前
striver (楼主) 4年前
JerryBool 4年前

offsetUnset 方法是 ArrayAccess 接口的方法的实现,可以把对象像数组一样进行访问。正确的使用方法是这样的:

unset($request['password']);

至于为什么没有过滤掉 password,可以断点调试下源码。

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!