直接看下面的註釋中針對每個文件的做用說明。php
<?php /** * BrowserKit - Make internal requests to your application. * * If you need to make requests to external sites and applications, consider using Goutte. * * Request.php 是一個簡單包裝請求中的各部分信息的容器,以提供存取。 * Response.php 是一個簡單包裝 content, status, headers 的對象,僅僅用於返回,return new Response()。 * Cookie.php 是一個 cookie 信息的容器,操做邏輯和原生cookie函數基本一致,能夠理解爲只在程序中傳遞的 cookie,不是在瀏覽器中真實設置的 cookie。 * CookieJar.php 是全部不重複未過時 Cookie對象 的容器,內部設置原理是 $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; * History.php 是記錄每一個 Request對象 的容器,以提供存取,內部設置原理是 $this->stack[] = clone $request; $this->position = count($this->stack) - 1; * Client.php 一個模擬瀏覽器的客戶端,內部組合應用 Request, Cookie, CookieJar, History 以及 DomCrawler, Process 組件,Process 實際請求腳本,DomCrawler 實際處理 HTML 文檔。 * * 以上容器的概念等同於對象,提供OOP的操做。 * * @see https://symfony.com/doc/current/components/browser_kit.html * @author <www.farwish.com> */ use Symfony\Component\BrowserKit\Client as BaseClient; use Symfony\Component\BrowserKit\Response; include __DIR__ . '/../../../vendor/autoload.php'; // Creating a Client. class Client extends BaseClient { protected function doRequest($request) { return new Response(); } } // Making Requests. $client = new Client(); $crawler = $client->request('GET', '/'); print_r($crawler);
小結:html
我的感受 BrowserKit 是一個比較雞肋的組件,主要用在 application 內部請求以及HTML解析,並且它的功能都是來源於其它兩個組件 Process 和 DomCrawler。git
若是要模擬請求外部站點,可以使用 Goutte 組件,它的功能來源於其它兩個大組件 Guzzle 和 DomCrawler,因此文檔用法並不詳細,原理同 BrowserKit 是一層再包裝。github
Code:https://github.com/farwish/php-lab/blob/master/lab/symfony/BrowserKit/client.php瀏覽器