Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

PSR7-Usage.md

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 5.36 KiB


001  ### PSR-7 Usage
002   
003  All PSR-7 applications comply with these interfaces 
004  They were created to establish a standard between middleware implementations.
005   
006  > `RequestInterface`, `ServerRequestInterface`, `ResponseInterface` extend `MessageInterface`  because the `Request` and the `Response` are `HTTP Messages`.
007  > When using `ServerRequestInterface`, both `RequestInterface` and `Psr\Http\Message\MessageInterface` methods are considered.
008   
009   
010  The following examples will illustrate how basic operations are done in PSR-7.
011   
012  ##### Examples
013   
014   
015  For this examples to work (at least) a PSR-7 implementation package is required. (eg: zendframework/zend-diactoros, guzzlehttp/psr7, slim/slim, etc)
016  All PSR-7 implementations should have the same behaviour.
017   
018  The following will be assumed: 
019  `$request` is an object of `Psr\Http\Message\RequestInterface` and
020   
021  `$response` is an object implementing `Psr\Http\Message\RequestInterface`
022   
023   
024  ### Working with HTTP Headers
025   
026  #### Adding headers to response:
027   
028  ```php
029  $response->withHeader('My-Custom-Header', 'My Custom Message');
030  ```
031   
032  #### Appending values to headers
033   
034  ```php
035  $response->withAddedHeader('My-Custom-Header', 'The second message');
036  ```
037   
038  #### Checking if header exists:
039   
040  ```php
041  $request->hasHeader('My-Custom-Header'); // will return false
042  $response->hasHeader('My-Custom-Header'); // will return true
043  ```
044   
045  > Note: My-Custom-Header was only added in the Response
046   
047  #### Getting comma-separated values from a header (also applies to request)
048   
049  ```php
050  // getting value from request headers
051  $request->getHeaderLine('Content-Type'); // will return: "text/html; charset=UTF-8"
052  // getting value from response headers
053  $response->getHeaderLine('My-Custom-Header'); // will return:  "My Custom Message; The second message"
054  ```
055   
056  #### Getting array of value from a header (also applies to request)
057  ```php
058  // getting value from request headers
059  $request->getHeader('Content-Type'); // will return: ["text/html", "charset=UTF-8"]
060  // getting value from response headers
061  $response->getHeader('My-Custom-Header'); // will return:  ["My Custom Message",  "The second message"]
062  ```
063   
064  #### Removing headers from HTTP Messages
065  ```php
066  // removing a header from Request, removing deprecated "Content-MD5" header
067  $request->withoutHeader('Content-MD5'); 
068   
069  // removing a header from Response
070  // effect: the browser won't know the size of the stream
071  // the browser will download the stream till it ends
072  $response->withoutHeader('Content-Length');
073  ```
074   
075  ### Working with HTTP Message Body
076   
077  When working with the PSR-7 there are two methods of implementation:
078  #### 1. Getting the body separately
079   
080  > This method makes the body handling easier to understand and is useful when repeatedly calling body methods. (You only call `getBody()` once). Using this method mistakes like `$response->write()` are also prevented.
081   
082  ```php
083  $body = $response->getBody();
084  // operations on body, eg. read, write, seek
085  // ...
086  // replacing the old body
087  $response->withBody($body); 
088  // this last statement is optional as we working with objects
089  // in this case the "new" body is same with the "old" one
090  // the $body variable has the same value as the one in $request, only the reference is passed
091  ```
092   
093  #### 2. Working directly on response
094   
095  > This method is useful when only performing few operations as the `$request->getBody()` statement fragment is required
096   
097  ```php
098  $response->getBody()->write('hello');
099  ```
100   
101  ### Getting the body contents
102   
103  The following snippet gets the contents of a stream contents.
104  > Note: Streams must be rewinded, if content was written into streams, it will be ignored when calling `getContents()` because the stream pointer is set to the last character, which is `\0` - meaning end of stream.
105  ```php 
106  $body = $response->getBody();
107  $body->rewind(); // or $body->seek(0);
108  $bodyText = $body->getContents();
109  ```
110  > Note: If `$body->seek(1)` is called before `$body->getContents()`, the first character will be ommited as the starting pointer is set to `1`, not `0`. This is why using `$body->rewind()` is recommended.
111   
112  ### Append to body
113   
114  ```php
115  $response->getBody()->write('Hello'); // writing directly
116  $body = $request->getBody(); // which is a `StreamInterface`
117  $body->write('xxxxx');
118  ```
119   
120  ### Prepend to body
121  Prepending is different when it comes to streams. The content must be copied before writing the content to be prepended.
122  The following example will explain the behaviour of streams.
123   
124  ```php
125  // assuming our response is initially empty
126  $body = $repsonse->getBody();
127  // writing the string "abcd"
128  $body->write('abcd');
129   
130  // seeking to start of stream
131  $body->seek(0);
132  // writing 'ef'
133  $body->write('ef'); // at this point the stream contains "efcd"
134  ```
135   
136  #### Prepending by rewriting separately
137   
138  ```php
139  // assuming our response body stream only contains: "abcd"
140  $body = $response->getBody();
141  $body->rewind();
142  $contents = $body->getContents(); // abcd
143  // seeking the stream to beginning
144  $body->rewind();
145  $body->write('ef'); // stream contains "efcd"
146  $body->write($contents); // stream contains "efabcd"
147  ```
148   
149  > Note: `getContents()` seeks the stream while reading it, therefore if the second `rewind()` method call was not present the stream would have resulted in `abcdefabcd` because the `write()` method appends to stream if not preceeded by `rewind()` or `seek(0)`.
150   
151  #### Prepending by using contents as a string
152  ```php
153  $body = $response->getBody();
154  $body->rewind();
155  $contents = $body->getContents(); // efabcd
156  $contents = 'ef'.$contents;
157  $body->rewind();
158  $body->write($contents);
159  ```
160