Download the PHP package grogorick/php-routing without Composer
On this page you can find all versions of the php package grogorick/php-routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download grogorick/php-routing
More information about grogorick/php-routing
Files in grogorick/php-routing
Package php-routing
Short Description Minimal PHP routing with zero dependencies
License
Informations about the package php-routing
Minimal PHP Routing
Set callbacks for hierarchical routes in a simple associative array structure.
Setup
Example
Routes Array Syntax
route-literal => [subroutes array]
static route literal string => subroutes array
/regex/ => subroutes function
regex to match a URL parameter => subroutes function getting the parsed parameter, generating a subroutes array
(subroutes group) => subroutes function
subroutes group name in parentheses => subroutes function generating a subroutes array
METHOD => callable
request METHOD in full uppercase => action callable to be called for this route
Action callables will be called with the following arguments:
URL parameters — in their order withing the respective route
form data — via$_POST
orfile_get_contents('php://input')
search parameters — via$_GET
Reference
route($routes)
Main function to parse the current request URL and call the corresponding callback function.
$routes (associative array)
— route literal string => subroute array
— route parameter regex => closure with parsed parameter as argument, returning a subroute array
— request method (POST/GET/PUT/PATCH/DELETE) => callback function
respond($response, $code = 200)
Helper function to output the retrieved response as JSON-encoded string, and set an optional status code. Stops execution afterwards.
$response (any) — JSON-serializable response object
$code (int) — HTTP response status code
Check($check, $subroutes)
Wrapper for subroutes array with restricted access.
$check (callable) — callback function to check for access permission
$subroutes — see route($routes)
respond_error_if_no_other_route_matches($error)
Report error duringCheck(...)
without directly stopping routing.
$error (string) — error message
Param($convert, $subroutes)
Wrapper for subroutes array to convert a parsed url parameter.
$convert (callable) — callback function to convert the latest parameter
$subroutes — see route($routes)
IntParam($subroutes)
Shorthand forParam('intval', $subroutes)
$subroutes — see route($routes)
Entity($subroutes)
Item($subroutes)
Wrapper for subroutes array to generate recommended response status codes for undefined request methods.
$subroutes — see route($routes)
set_response_headers($headers)
add_response_header($header)
Replace/add headers that are automatically applied when using respond(...).
$headers (array) — headers to replace all default/previously set headers
$header (string) — header to add
set_options($options)
Set options available in \Options.
$options (associative array) — options to set, using values from \Options as array keys
Server Configuration
Additional Request Methods
By default, most Apache configurations only allow GET and POST requests. Add the following to allow further methods (PUT, PATCH, DELETE, HEAD, OPTIONS).
For this to work, the httpd.conf
should include
Response Headers
Response headers can be set either via PHP, which allows to set them dynamically, e.g., to support multiple specific origins:
or via .htaccess
if static settings are sufficient:
URL Syntax
Short
https://your-api.domain /v1/accounts/42
Medium
https://your-api.domain /api.php/v1/accounts/42
(pre-configured on most systems)
or
Long
https://your-api.domain /api.php?request=/v1/accounts/42
(vanilla PHP)