@@ -30,7 +30,20 @@ Contributions and suggestions are welcome.
30
30
31
31
## Usage
32
32
33
- Below is an example of a simple 'Hello, World!' FastCGI application in PHP.
33
+ Below is an example of a simple 'Hello, World!' FastCGI application. There are
34
+ 3 examples. One with the PHPFastCGI request, one with Symfony HTTP Foundation request
35
+ and one with PSR-7 Request.
36
+
37
+ ### PHPFastCGI request
38
+
39
+ Using the pure PHPFastCGI is the fastest method since it does not involve any converting
40
+ of requests.
41
+
42
+ We need a PSR-7 implementation's response object.
43
+
44
+ ```
45
+ composer require composer require zendframework/zend-diactoros
46
+ ```
34
47
35
48
``` php
36
49
<?php // fastCGI_app.php
@@ -43,9 +56,7 @@ use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
43
56
use Zend\Diactoros\Response\HtmlResponse;
44
57
45
58
// A simple kernel. This is the core of your application
46
- $kernel = function (RequestInterface $request) {
47
- // $request->getServerRequest() returns PSR-7 server request object
48
- // $request->getHttpFoundationRequest() returns HTTP foundation request object
59
+ $kernel = function (RequestInterface $request) {
49
60
return new HtmlResponse('<h1 >Hello, World!</h1 >');
50
61
};
51
62
@@ -56,6 +67,73 @@ $application = (new ApplicationFactory)->createApplication($kernel);
56
67
$application->run();
57
68
```
58
69
70
+ ### Symfony HTTP Foundation Request
71
+
72
+ Use this when your application is taking advantage of the Symfony echosystem.
73
+
74
+ ```
75
+ composer require symfony/http-foundation
76
+ ```
77
+
78
+ ``` php
79
+ <?php // fastCGI_app.php
80
+
81
+ require_once dirname(__FILE__) . '/../vendor/autoload.php';
82
+
83
+ use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
84
+ use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
85
+ use Symfony\Component\HttpFoundation\Response;
86
+
87
+ $kernel = function (RequestInterface $request) {
88
+ $sfRequest = $request->getHttpFoundationRequest(); // returns HTTP Foundation request object
89
+
90
+ return new Response('<h1 >Hello, World!</h1 >' . $sfRequest->getUri());
91
+ };
92
+
93
+ $application = (new ApplicationFactory)->createApplication($kernel);
94
+ $application->run();
95
+ ```
96
+
97
+ ### PSR-7 Request
98
+
99
+ Here is the same example but with PSR-7 HTTP objects. First you need to install any PSR-17 (HTTP Factory) implementation
100
+ and then a PSR-17 utility library ([ nyholm/psr7-server] ( https://github.com/nyholm/psr7-server ) ).
101
+
102
+ ```
103
+ composer require http-interop/http-factory-diactoros nyholm/psr7-server
104
+ ```
105
+
106
+ ``` php
107
+ <?php // fastCGI_app.php
108
+
109
+ require_once dirname(__FILE__) . '/../vendor/autoload.php';
110
+
111
+ use Http\Factory\Diactoros;
112
+ use Nyholm\Psr7Server\ServerRequestCreator;
113
+ use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
114
+ use PHPFastCGI\FastCGIDaemon\Http\Request;
115
+ use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
116
+ use Zend\Diactoros\Response\HtmlResponse;
117
+
118
+ // Give the Request an instance of ServerRequestCreatorInterface filled with PSR-17 factories.
119
+ // This is how we are independent of any PSR-7 implementation.
120
+ Request::setServerRequestCreator(new ServerRequestCreator(
121
+ new Diactoros\ServerRequestFactory,
122
+ new Diactoros\UriFactory,
123
+ new Diactoros\UploadedFileFactory,
124
+ new Diactoros\StreamFactory
125
+ ));
126
+
127
+ $kernel = function (RequestInterface $request) {
128
+ $psr7Request = $request->getServerRequest(); // returns PSR-7 ServerRequestInterface
129
+
130
+ return new HtmlResponse('<h1 >Hello, World!</h1 >' . $psr7Request->getRequestTarget());
131
+ };
132
+
133
+ $application = (new ApplicationFactory)->createApplication($kernel);
134
+ $application->run();
135
+ ```
136
+
59
137
## Server Configuration
60
138
61
139
### NGINX
0 commit comments