Download the PHP package virge/api without Composer
On this page you can find all versions of the php package virge/api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api
Virge::Api
Virge::Api allows the simple creation of API endpoints.
Virge::Api allows defining get/post/put/delete methods, as well as chaining API authentication methods by defining custom verifiers.
Getting Started
To use Virge::Api you should start with a default Virge::Project
This will create an empty Virge::Reactor, and create the skeleton required to setup the Api.
The entire directory needs to exist on your webserver, but the entry directory should be the ./public directory, this can be symlinked:
This can also be done using docker:
Once the container is up, you'll need to run the symlink command like above
Virge::Project also requires rewrites, for apache you can set that up by creating myproject.conf in /etc/httpd/conf.d/
Restarting the docker container or apache will then allow you to visit localhost:3000 in your browser. You will get a 500 error, a blank white page, or a 404 page depending on configuration. This is because there is no routing or routes setup.
Hello Api
We need to include Virge::Api as a capsule, and add the package to our project:
To create our first API route, we need to create our First Capsule. A Capsule is registered with the Reactor and will automatically load configuration files for us, loading services, controllers, and api definition files.
Under the src directory we can create a MyProject directory We will need to create MyProjectCapsule.php under this directory:
Now we need to add our Capsule to app/Reactor.php, add both the MyProject\MyProjectCapsule() as well as the Virge\Api\Capsule()
While in this file, we can also define what API version(s) are active. after our parent::registerCapsules call, let's enable version 1 of our Api:
We also need to tell composer to automatically load our capsule, update bootstrap.php to add our namespace:
Now we can register our API Method, creating the file src/MyProject/resources/config/api.php
Visiting http://localhost:3000/api/v/1/hello should now return a json response with "world"
Api's can also call controller functions, and the methods are passed in the Virge::Router Request object, allowing you to get the JSON request body, GET and POST Fields, as well as URL parameters.
Api Verification
You can define custom API verifiers, which are simply callback functions that return true or false to allow access For example, to update our hello API endpoint to require an api key, we can add a verifier: In config/api.php
Visting http://localhost:3000/api/v/1/hello in your browser will now return a non successful response, but adding in the apiKey: http://localhost:3000/api/v/1/hello?apiKey=123 Will allow you to visit the page.
A method can have as many verifiers as you want, and ALL verifiers must return true for the function to be called and the method to be successful.
Api Parameters
Api method names can also contain URL Parameters, these parameters are accessed via the request object:
Visiting http://localhost:3000/api/v/1/hello/bob will now return a JSON body with "Hello: bob"