Download the PHP package fostam/simplerouter without Composer

On this page you can find all versions of the php package fostam/simplerouter. 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 simplerouter

fostam/simplerouter

Simple PHP router, mainly for building API backends.

Features

Install

The easiest way to install SimpleRouter is by using composer:

Usage

This is how a typical "handler" would look like:

This could be a (very simplified) processor class for getting users:

An example class for creating a user:

Reference

Router

setOption()

Router setOption($option, $value) Set a router option. The router object is returned to allow chaining. Following options are available:

Option Value Default Value
Router::OPT_REQUEST_PATH_PREFIX prefix that is truncated from the path ""
Router::OPT_RESPONSE_TYPE_DEFAULT default response type Response::TYPE_HTML
Router::OPT_KEY_ERROR_MESSAGE key in result JSON for error message "error.message"
Router::OPT_KEY_ERROR_CODE key in result JSON for error code "error.code"
Router::OPT_INTERNAL_ERROR_MSG error message text for internal errors "internal server error"
Router::OPT_CORS_PERMISSIVE permissive CORS mode false

addRoute()

Route addRoute(Route $route)

Add a Route object to the list of routes. For convenience, the route object is returned to allow direct calling of Route methods.

createRoute()

Route createRoute(string $path, string $method, mixed $processor)

Creates a Route object from the given parameters, adds it to the list of routes and returns it. See the Route documentation for a description of the parameters.

resolve()

void resolve(string $path = '', string $method = '')

Resolve the given path/method and execute the matching processor. If $path is empty, $_SERVER['SCRIPT_NAME'] is used as path. If $method is empty, $_SERVER['REQUEST_METHOD'] is used as method.

sendResult()

void sendResult()

Sends the result. This include the HTTP status code, HTTP headers (including content type) and the body. Content type header and body format depend on the configured response type.

getResponseObject()

Response getResponseObject()

Get the Response object. The response object represents what is sent when calling sendResult().

Route

Constructor

Route __construct(array $config)

The constructor is the preferred way of creating a route when the route configuration comes from a dynamic source, e.g. from a database.

It builds a route object with the given associative array $config. The following configuration keys are available:

Key Value Mandatory
Route::PATH path used for matching yes
Route::METHOD method used for matching yes
Route::PROCESSOR processor that is executed on a match yes

The path is the endpoint that defines when this route matches. When comparing to the actual path, the Router::OPT_REQUEST_PATH_PREFIX is prefixed, if configured.

The method is the HTTP method that needs to match along with the path, e.g. Http::METHOD_GET or Http::METHOD_POST.

If both path and method match, the processor is called. There are three different possibilities to define the processor:

In either case, the class/object passed is required to extend the abstract Processor class.

create()

Route ::create(string $path, string $method, mixed $processor)

Static method to create a Route object from parameters. The create() method is the preferred way of creating a route when the route definitions are hardwired in PHP. Refer to the constructor for a description of the parameters.

Processor

getPath()

string getPath()

Get the path this processor is configured for.

getMethod()

string getMethod()

Get the HTTP method this processor is configured for.

getResponseObject()

Response getResponseObject()

Get the response object that will be used to create the response after the processor has finished.

setResponseCode()

void setResponseCode(int $code)

Set the HTTP response code, e.g. Http::CODE_OK (200).

Shorthand for getResponseObject()->setCode($code).

setResponseType()

void setResponseType(string $type)

Set the HTTP response type, e.g. Response::TYPE_JSON. This can be either one of the TYPE_ constants from the Response class, or any valid MIME type string, e.g. application/xml. If no response type is set, the router will send the default response type (Router::OPT_RESPONSE_TYPE_DEFAULT), if set. If no default is set, text/html is sent.

Shorthand for getResponseObject()->setType($type).

setResponseData()

void setResponseData(mixed $data)

Set the body data. This can be either an array for the Response::TYPE_JSON response type, or a string for Response::TYPE_HTML or any other response type.

Shorthand for getResponseObject()->setData($data).

getPathParams()

string[] getPathParams()

Get all path parameter values as key/values pairs.

Example:

A call to getPathParams() in the Test processor call will return:

getPathParam()

string getPathParam(string $param)

Get the path parameter value for a single parameter. For the example above, getPathParam('name') would return john.

getQueryParams()

string[] getQueryParams()

Return the query parameters as key/value pairs. This is equivalent to PHP's $_GET superglobal.

getQueryParam()

string getQueryParam(string $param)

Get the query parameter value for a single parameter.

getPostParams()

string[] getPostParams()

Return the POST parameters as key/value pairs. This is equivalent to PHP's $_POST superglobal.

getPostParam()

string getPostParam(string $param)

Get the POST parameter value for a single parameter.

getJSONPostData()

mixed getJSONPostData()

When the body of a POST, PUT or PATCH call contains a JSON string, it's structure can be retrieved as a PHP array.

Example:

A call to getJSONPostData() will return this array:

Response

setCode()

void setCode(int $code)

Set the HTTP response code, e.g. Http::CODE_OK (200).

getCode()

int getCode()

Get the current response code.

setType()

void setType(string $type)

Set the HTTP response type, e.g. Response::TYPE_JSON. This can be either one of the TYPE_ constants from the Response class, or any valid MIME type string, e.g. application/xml. If no response type is set, the router will send the default response type (Router::OPT_RESPONSE_TYPE_DEFAULT), if set. If no default is set, text/html is sent.

getType()

int getType()

Get the current response code.

setData()

void setData(mixed $data)

Set the body data. This can be either an array for the Response::TYPE_JSON response type, or a string for Response::TYPE_HTML or any other response type.

getData()

mixed getData()

Get the current data.

setLocationPath()

void setLocationPath(string $path)

Set a location path. This path prefixed with Router::OPT_REQUEST_PATH_PREFIX, if set, and then sent as Location header.

Example:

getLocationPath()

string getLocationPath()

Get the location path.

setHeader()

void setHeader(string $header, string $value, bool $append = false)

Set the HTTP header $header to the value $value. If $append is true and the header had been set before, an additional header line for the same header is added.

isHeaderSet()

bool isHeaderSet(string $header)

Returns whether the header $header has been set or not.

getHeader()

array getHeader(string $header)

Get the header $header. An array with the following elements is returned:

getHeaders()

array getHeaders()

Get all headers. It returns an array of entries as returned by getHeader().

clearHeader()

void clearHeader(string $header)

Clear the header $header.

clearHeaders()

void clearHeaders()

Clear all headers.

addResponseData()

void addResponseData(string $key, string $value, string $separator = '.')

Adds data to the response array (for the JSON response type). Sub-array keys can be separated by the $separator string.

Example:

would set this value:

corsSetOrigin()

void corsSetOrigin(string $origin)

Set the Access-Control-Allow-Origin to $origin, e.g. corsSetOrigin('*') to allow all origins.

corsAddOrigin()

void corsAddOrigin(string $origin)

If multiple origins are required, an origin can be added, e.g. corsAddOrigin('example.org'). The list of origins is compared to the Origin request header. On a match, the Access-Control-Allow-Origin is set to the matching origin.

corsGetOrigins()

array corsGetOrigins()

Get the list of allowed origins.

corsAllowCredentials()

void corsAllowCredentials(bool $allowed)

Used to set the Access-Control-Allow-Credentials header.

corsAddAllowedHeader()

void corsAddAllowedHeader(string $header)

Add a header to the list of allowed headers used for Access-Control-Allow-Headers.

corsAddAllowedHeaders()

void corsAddAllowedHeaders(string[] $headers)

Set an array of allowed headers.

corsSetMaxAge()

void corsSetMaxAge(int $maxAgeSeconds)

Set the maximum age in seconds used for the Access-Control-Max-Age header.


All versions of simplerouter with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6
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 fostam/simplerouter contains the following files

Loading the files please wait ....