Skip to content

Commit a3d158e

Browse files
yamiko-ninjajaviereguiluz
authored andcommitted
added form submissions and moved creating a client to top
1 parent 74cfecd commit a3d158e

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

components/browser_kit/introduction.rst

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ Basic Usage
2323
.. note::
2424
The component only provides an abstract client and does not provide any "default" backend for the HTTP layer.
2525

26+
Creating a Client
27+
-----------------
28+
29+
To create your own client you must extend the abstract client class and implement the doRequest method.
30+
This method accepts a request and should return a response.
31+
32+
.. code-block:: php
33+
34+
namespace ACME;
35+
36+
use Symfony\Component\BrowserKit\Client as BaseClient;
37+
use Symfony\Component\BrowserKit\Response;
38+
39+
class Client extends BaseClient {
40+
protected function doRequest($request) {
41+
// convert request into a response
42+
// ...
43+
return new Response($content, $status, $headers);
44+
}
45+
}
46+
47+
For a simple implementation of a browser based on an HTTP layer, have a look at Goutte_.
48+
49+
For an implementation based on HttpKernelInterface, have a look at the Client provided by the :doc:`/components/http_kernel/introduction`.
50+
51+
2652
Making Request
2753
~~~~~~~~~~~~~~
2854

@@ -52,8 +78,23 @@ Select a link with the crawler and pass it to the click method to click on the l
5278
$client->click($link);
5379
5480
Submiting Forms
55-
~~~~~~~~~~~~~~~~
81+
~~~~~~~~~~~~~~~
82+
83+
.. code-block:: php
5684
85+
use ACME\Client;
86+
87+
// make a real request to an external site
88+
$client = new Client();
89+
$crawler = $client->request('GET', 'https://github.com/login');
90+
91+
// select the form and fill in some values
92+
$form = $crawler->selectButton('Log in')->form();
93+
$form['login'] = 'symfonyfan';
94+
$form['password'] = 'anypass';
95+
96+
// submit that form
97+
$crawler = $client->submit($form);
5798
5899
Cookies
59100
-------
@@ -64,29 +105,5 @@ History
64105
Insulated Request
65106
-----------------
66107

67-
Creating a Client
68-
-----------------
69-
70-
To create your own client you must extend the abstract client class and implement the doRequest method.
71-
This method accepts a request and should return a response.
72-
73-
.. code-block:: php
74-
namespace ACME;
75-
76-
use Symfony\Component\BrowserKit\Client as BaseClient;
77-
use Symfony\Component\BrowserKit\Response;
78-
79-
class Client extends BaseClient {
80-
protected function doRequest($request) {
81-
// convert request into a response
82-
// ...
83-
return new Response($content, $status, $headers);
84-
}
85-
}
86-
87-
For a simple implementation of a browser based on an HTTP layer, have a look at Goutte_.
88-
89-
For an implementation based on HttpKernelInterface, have a look at the Client provided by the :doc:`/components/http_kernel/introduction`.
90-
91108
.. _Packagist: https://packagist.org/packages/symfony/browser-kit
92109
.. _Goutte: https://github.com/fabpot/Goutte

0 commit comments

Comments
 (0)