in LaravelPHP with 8 comments





$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()



public static function capture()
//        这个方法主要是返回一个request的对象
//        设置$httpMethodParameterOverride变量,后面会用到这个判断
//        1.首先我们先执行SymfonyRequest::createFromGlobals(),这个方法其实会帮我们返回一个http-foundation/Question.php生成的request实例
        return static::createFromBase(SymfonyRequest::createFromGlobals());


private static function createRequestFromFactory(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
//        self::$requestFactory如果有设置的话,是一个闭包,会直接执行,返回一个request的对象,其实在RequestTest中有所调用,返回一个NewRequest,而这个对象继承自Request
        if (self::$requestFactory) {
            $request = call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);

            if (!$request instanceof self) {
                throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');

            return $request;
//如果不存在的情况下,会直接执行,new一个对象,不过需要注意的是,这里并非直接new Request,而是用的static,static是php的一个语法糖:静态绑定,比如我现在有另外一个TestRequest继承自Request,如果这里使用的是new static,返回的就是TestRequest的实例了
        return new static($query, $request, $attributes, $cookies, $files, $server, $content);


$request = (new static)->duplicate(
            $request->query->all(), $request->request->all(), $request->attributes->all(),
            $request->cookies->all(), $request->files->all(), $request->server->all()

这是createFromBase方法中的一段代码,这里需要说明一下的是,因为createFromBase方法是一个静态方法,所以在这个方法中我们无法使用$this,只能通过new static的方式来转换。我们可以看到,此时调用了当前对象的duplicate的方法,而其中诸如$request->query->all()方法,都是返回我们之前在生成Symfony\Component\HttpFoundation\Request时提到的初始化的方法所绑定在$parameters数组中的内容。

public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
        $dup = clone $this;
        if ($query !== null) {
            $dup->query = new ParameterBag($query);
        if ($request !== null) {
            $dup->request = new ParameterBag($request);
        if ($attributes !== null) {
            $dup->attributes = new ParameterBag($attributes);
        if ($cookies !== null) {
            $dup->cookies = new ParameterBag($cookies);
        if ($files !== null) {
            $dup->files = new FileBag($files);
        if ($server !== null) {
            $dup->server = new ServerBag($server);
            $dup->headers = new HeaderBag($dup->server->getHeaders());
        $dup->languages = null;
        $dup->charsets = null;
        $dup->encodings = null;
        $dup->acceptableContentTypes = null;
        $dup->pathInfo = null;
        $dup->requestUri = null;
        $dup->baseUrl = null;
        $dup->basePath = null;
        $dup->method = null;
        $dup->format = null;

        if (!$dup->get('_format') && $this->get('_format')) {
            $dup->attributes->set('_format', $this->get('_format'));

        if (!$dup->getRequestFormat(null)) {

        return $dup;


public function __clone()
        $this->query = clone $this->query;
        $this->request = clone $this->request;
        $this->attributes = clone $this->attributes;
        $this->cookies = clone $this->cookies;
        $this->files = clone $this->files;
        $this->server = clone $this->server;
        $this->headers = clone $this->headers;


Comments are closed.
  1. Wow that was odd. I just wrote an very long comment but after I clicked submit my comment didn't show up.

    Grrrr... well I'm not writing all that over again. Anyways, just
    wanted to say superb blog!

  2. My partner and I absolutely love your blog and find almost all of your
    post's to be exactly I'm looking for. Do you offer guest writers to write content
    in your case? I wouldn't mind creating a post or elaborating
    on a few of the subjects you write with regards to here.
    Again, awesome site!

  3. It is really a nice and helpful piece of information. I am glad that
    you just shared this useful info with us. Please stay us informed like this.
    Thank you for sharing.

  4. It's not my first time to pay a quick visit this site, i am visiting this website dailly and get fastidious facts from here all
    the time.

  5. What's up to all, how is the whole thing, I think every one is
    getting more from this site, and your views are
    fastidious in support of new users.

  6. If some one needs to be updated with most recent technologies afterward he must be go to
    see this web site and be up to date all the time.

  7. It's very easy to find out any topic on net as compared to
    books, as I found this post at this site.

  8. WOW just what I was searching for. Came here by searching for PHP