Download the PHP package aryelgois/medools-router without Composer

On this page you can find all versions of the php package aryelgois/medools-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package medools-router

Medools Router

Index:

Intro

A Router framework to bootstrap RESTful APIs based on Medools.

Routes

Basically, all routes are formatted as:

See bellow details about the resource name.

The resource id depends on the PRIMARY_KEY defined in the model. Usually it is an integer, but if there is a composite PRIMARY_KEY, each column must be in the id, separated by primary_key_separator, e.g. /resource/1-1.

Also, resources can be nested:

Nesting is only allowed in resources (not in collections: /resource/resource_1 is wrong), but the route can end in a collection. And there is no hard limit of nesting levels, the only requirement is that the last resource (or collection) has a foreign key to the previous resource, that has a foreign key to the previous one, and so on. The id is only used in the first resource.

* If resource_1 has multiple foreign columns for resource, only the first one is used. As a work around, you can use Collection query parameters to filter the correct column

Root Route

When requesting the / route, a count of all resources is returned. If the meta config is not empty, it is also included. An OPTIONS request just lists all implemented methods.

Install

Open a terminal in your project directory and run:

composer require aryelgois/medools-router

Setup

Medools requires a config file to connect to your database. (see more here)

Additionally, you will need some configurations and a resources list for your API.

Also, if using authentication, you will need a secret file (or multiple files) and a Sign up system (or an administrator will add valid credentials).

Configurations

The Router accepts an array of configurations that will be passed to properties in it. The array may contain:

Resources list

A map of resource names to Medools Models, and optionally to specific configurations for that resource.

The resource names should be plural. You should choose the casing convention to be camelCase or snake_case. Prefer keeping consistent with the columns casing in your models.

The value mapped can be either a string with the Fully Qualified Model Class, or an array with:

Usage

Follow the example to see how to use this framework in your web server.

When a request is done, your web server must redirect to a php script. If you are using Apache, there is already a .htaccess in the example that does the job.

First, the script requires Composer's autoloader and loads Medools config. Then it gathers request data:

Finally, a Router object is created with a resources list, optional configurations and all that data from the request. It will do its best to solve the route and give a response.

The resources list and configurations can be stored in a external file, like .json or .yml or something else. Remember to add a library to parse that file before passing to the Router.

You can also configure a subdomain like api.example.com to handle the routes. It is up to you.

SECURITY NOTE: It is highly recommended that you use SSL to protect your data transactions

HTTP Methods

The following HTTP methods are implemented by the Router class:

Some notes:

Query parameters

The client can use some query parameters for a more precise request:

Collection request

Operator Medoo counterpart Name
gt > Greater Than
ge >= Greater or Equal to
lt < Lesser Than
le <= Lesser or Equal to
ne ! Not Equal
bw <> BetWeen
nw >< Not betWeen
lk ~ LiKe
nk !~ Not liKe
rx REGEXP RegeXp

Examples:

Notes:

Resource request

Collection or Resource

Cache

If enabled, caching headers ETag and Cache-Control are sent with successful responses that have a body.

This functionality can be enabled globally or per resource.

If the client sends If-None-Match Header, it is tested to provide a 304 Not Modified response.

Authentication and Authorization

By default, all resources are public. Defining the authentication config makes all resources private and to access them the client must authenticate. In this case, some resources may define themselves as public, or allow a few methods for unauthenticated requests.

To authenticate, first the client must provide some credentials to the Router with a Basic Authorization Header. In the example, it is at the /auth route. If authenticated, the server sends back a JWT token. This token must be sent to all the other routes with a Bearer Authorization Header until it expires. When it happens, the client must authenticate again.

Authentication credentials ("Accounts") are stored in the authentications table, with the columns:

Another table, authorizations, maps accounts to authorized resources. It can also list authorized methods and can include a filter passed directly to Medoo, to limit which entries are authorized.

These two tables are be filled by your Sign up system, and controlled by an user panel or an administrator.

Errors

When possible, the Router will return an error response with an appropriate HTTP Status code and a JSON payload with code and message keys. Other keys may appear, but are optional.

Error table:

Code Name Status Description
0 ERROR_UNKNOWN_ERROR 500 Unknown error placeholder
1 ERROR_INTERNAL_SERVER 500 The Router detected a Server error
2 ERROR_INVALID_CREDENTIALS 401 Authorization Header is invalid
3 ERROR_UNAUTHENTICATED 401 Request could not be authenticated
4 ERROR_INVALID_TOKEN 401 Authorization token is invalid
5 ERROR_METHOD_NOT_IMPLEMENTED 501 Requested Method is not implemented in the Router
6 ERROR_UNAUTHORIZED 403 Requested resource is not public and the client does not have authorization to access it
7 ERROR_INVALID_RESOURCE 400 Route contains invalid resource
8 ERROR_INVALID_RESOURCE_ID 400 Route contains invalid resource id
9 ERROR_INVALID_RESOURCE_OFFSET 400 Route contains invalid resource offset
10 ERROR_INVALID_RESOURCE_FOREIGN 400 Nested resource does not have foreign key to previous resource
11 ERROR_RESOURCE_NOT_FOUND 404 Requested resource does not exist in the Database
12 ERROR_UNSUPPORTED_MEDIA_TYPE 415 Request's Content-Type is not supported
13 ERROR_INVALID_PAYLOAD 400 Request's Content-Type has syntax error or is invalid
14 ERROR_METHOD_NOT_ALLOWED 405 Requested Method is not allowed for Route
15 ERROR_NOT_ACCEPTABLE 406 Requested resource can not generate content complying to Accept header
16 ERROR_INVALID_QUERY_PARAMETER 400 Query parameter has invalid data
17 ERROR_UNKNOWN_FIELDS 400 Resource does not have requested fields
18 ERROR_READONLY_RESOURCE 400 Resource can not be modified
19 ERROR_FOREIGN_CONSTRAINT 400 Foreign constraint fails

If an error or exception was not handled correctly, the response body is unpredictable and may depend on error_reporting.

Extending

You can extend the Router class to hack its code. Just remember to pass the correct class to the Controller.

Changelog


All versions of medools-router with dependencies

PHP Build Version
Package Version
Requires php Version ^7.0
ext-zlib Version *
aryelgois/utils Version ^0.5
aryelgois/medools Version ^5.1
aryelgois/yasql-php Version ^0.6.0
firebase/php-jwt Version ^5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package aryelgois/medools-router contains the following files

Loading the files please wait ....