'content' => $request ['content'] 为什么不是 $request->content ?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Status;
use Auth;

class StatusesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'content' => 'required|max:140'
        ]);

        Auth::user()->statuses()->create([
            'content' => $request['content']
        ]);
        session()->flash('success', '发布成功!');
        return redirect()->back();
    }
}

这里的 'content' => $request['content'] , $request之前都是用的属性 或者 方法, 这里当做数组使用, 是什么意思? 为什么不能使用$request->content?

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案
  • 首先说明这两种写法效果是一样的;

  • 下面看看源代码:基于\Illuminate\Http\Request::class查找的源码方法如下

    • $request->content:会触发__get魔术方法
    
    /**
     * Get an input element from the request.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        if (array_key_exists($key, $this->all())) {
            return data_get($this->all(), $key);
        }
    
        return $this->route($key);
    }
    • $request['content']:这种写法会触发ArrayAccess接口类的offsetGet方法
    
    /**
     * Get the value at the given offset.
     *
     * @param  string  $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->__get($offset);
    }
    • 从上可看出调用的其实是同一个方法
4年前 评论
讨论数量: 3
  • 首先说明这两种写法效果是一样的;

  • 下面看看源代码:基于\Illuminate\Http\Request::class查找的源码方法如下

    • $request->content:会触发__get魔术方法
    
    /**
     * Get an input element from the request.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        if (array_key_exists($key, $this->all())) {
            return data_get($this->all(), $key);
        }
    
        return $this->route($key);
    }
    • $request['content']:这种写法会触发ArrayAccess接口类的offsetGet方法
    
    /**
     * Get the value at the given offset.
     *
     * @param  string  $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->__get($offset);
    }
    • 从上可看出调用的其实是同一个方法
4年前 评论

同等效果吧

4年前 评论
TigerLin

$request 此刻是个array,$request->content 需要是obj

4年前 评论
  • 首先说明这两种写法效果是一样的;

  • 下面看看源代码:基于\Illuminate\Http\Request::class查找的源码方法如下

    • $request->content:会触发__get魔术方法
    
    /**
     * Get an input element from the request.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        if (array_key_exists($key, $this->all())) {
            return data_get($this->all(), $key);
        }
    
        return $this->route($key);
    }
    • $request['content']:这种写法会触发ArrayAccess接口类的offsetGet方法
    
    /**
     * Get the value at the given offset.
     *
     * @param  string  $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->__get($offset);
    }
    • 从上可看出调用的其实是同一个方法
4年前 评论

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