|
| 1 | +# CommonPHP Web Library |
| 2 | + |
| 3 | +The CommonPHP Web Library provides a suite of components designed to streamline the development of web applications by offering a standardized, easy-to-use set of utilities for handling HTTP requests and responses, along with robust exception handling. Inspired by the flexibility of PHP and the need for more straightforward, decoupled components in modern web development, this library aims to offer PHP developers a reliable toolkit for enhancing their web projects. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **HTTP Request Handling**: Simplifies the process of managing HTTP requests, including methods, schemes, headers, and cookies. |
| 8 | +- **HTTP Response Management**: Offers a structured way to create and manage HTTP responses, including status codes, headers, and body content. |
| 9 | +- **Exception Handling**: Provides specialized exceptions to handle common web development issues, improving the debuggability and reliability of your application. |
| 10 | +- **Support Classes**: Includes enums for request methods, schemes, and response statuses, making your code more readable and maintainable. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +You can install the CommonPHP Web Library using Composer: |
| 15 | + |
| 16 | +```bash |
| 17 | +composer require comphp/web |
| 18 | +``` |
| 19 | + |
| 20 | +Replace `your/package-name` with the actual package name of the CommonPHP Web Library on Packagist. |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Handling Requests |
| 25 | + |
| 26 | +To handle an HTTP request, you can easily instantiate a `Request` object: |
| 27 | + |
| 28 | +```php |
| 29 | +use CommonPHP\Web\Request; |
| 30 | + |
| 31 | +$request = Request::fromRequest(); |
| 32 | +``` |
| 33 | + |
| 34 | +This will automatically populate the `Request` object with details from the current HTTP request, including method, scheme, headers, and any parameters. |
| 35 | + |
| 36 | +### Creating Responses |
| 37 | + |
| 38 | +To create an HTTP response, you can use the `Response` class: |
| 39 | + |
| 40 | +```php |
| 41 | +use CommonPHP\Web\Response; |
| 42 | +use CommonPHP\Web\Support\ResponseStatus; |
| 43 | + |
| 44 | +$response = new Response( |
| 45 | + body: 'Hello, world!', |
| 46 | + status: ResponseStatus::SUCCESS_OK, |
| 47 | + headers: ['Content-Type' => 'text/plain'] |
| 48 | +); |
| 49 | + |
| 50 | +$response->send(); |
| 51 | +``` |
| 52 | + |
| 53 | +This will send a response with the specified body, status code, and headers. |
| 54 | + |
| 55 | +### Exception Handling |
| 56 | + |
| 57 | +The library includes several exceptions designed to handle common errors in web development: |
| 58 | + |
| 59 | +- `UndefinedRequestMethodException` |
| 60 | +- `UndefinedRequestSchemeException` |
| 61 | +- `UndefinedResponseStatusCodeException` |
| 62 | + |
| 63 | +These can be used to catch and respond to errors more effectively in your application. |
| 64 | + |
| 65 | +## Contributing |
| 66 | + |
| 67 | +We welcome contributions from the community, whether it's through submitting bug reports, proposing enhancements, or creating pull requests. Please refer to our CONTRIBUTING.md file for more details on how to contribute to the CommonPHP Web Library. |
| 68 | + |
| 69 | +## License |
| 70 | + |
| 71 | +This library is licensed under the [MIT License](LICENSE.md). |
0 commit comments