Download the PHP package patrick-barreto/http without Composer
On this page you can find all versions of the php package patrick-barreto/http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download patrick-barreto/http
More information about patrick-barreto/http
Files in patrick-barreto/http
Package http
Short Description This package will create the http service that will create endpoints, recive request and send response
License GPL-3.0-or-later
Informations about the package http
About Package
This package gonna make all the essential HTTP ressources for your API projetc.
How to install
How to Use
Important: Make sure that in your server configuration file you are redirecting all requests to index.php.
Basically you will use the Http abstration to manipulate all ressources from this package.
Roates
-
POST
-
GET
-
PUT
-
PATCH
- DELETE
Important: For the same http method declaration, the routes order must be:
- statics routes first,
- and dinamic routes last
Middlewares
Create a middleware
You need to import Middleware Interface to certificate that you were implemented a valid Middleware sintaxe for system.
Your middleware must be a class like this:
Important: This is the instance of request that you can manipulate and insert information from your system. This instance is accessible for you in the callback function parameter that you need to pass in the routes.
Use yours middlewares
To use your middlewares you need to fill the system with theirs namespaces and inform if the execution is default or not.
Add new middleware namespace to middlewares namespaces map
Default middlewares are executed for all routes.
important: Like the routes, the order of default middlewares declaration are very important. It will be the execution order. For middlewares not default, to use them you need to inform the name of middleware, in execution order to be executed after default middlewares.
Response
To send a response for anyone request you cant use Http::response, the method have default values for default responses.
Request Class
The Request Class store usefull data from the request. This informations are static and acessible in anywhere in your sistem.
During the middlewares you can increment your personal data into request intance class that will be give to you in callback param on routes declaration, where you will pass to your controller all request or specfic data.
Your specific data will not be a static data, will be a instance data. The static data will be access by these bellow methods
You can find here:
- HTTP Method
- HTTP Headers
- Request Route
- Query Strings
- Path Params
- Request Body
The first param in callback is reserved to request, you are free to name it as you want.
Methods
This method is responsable to return the request method
- return string
This method is responsable to return the request Headers
- return array
This method is responsable to return the request param data, path params and query strings
- return array|string
This method is responsable to set the query string data.
- return array
This method is responsable to get the path params
- return array
This method is responsable to return the request all data method
- return array
This method is responsable to return the request Body
-
return array
Run
- After routes and middlewares declaration and midlleware names mapped, start the aplication.
CORS
To configurate your CORS policies, you can use CORS Method.
Important: This method will make a global config CORS for all routes. If you need any more datailed configuration, you can create a Middleware and change an specific headers.
EXEMPLES:
Exemple to use this package
Middleware Delcaration
MiddlewaresMap.php
This file will fill Midleware Queue with middleware namespace and names.
Routes.php
This will be the file when we will make routes declarate. It can be with more than one file too..
To load all your routes to system you can use the
It will load all files from an path. Use it it before run app.