Download the PHP package ginger-tek/routy without Composer
On this page you can find all versions of the php package ginger-tek/routy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ginger-tek/routy
More information about ginger-tek/routy
Files in ginger-tek/routy
Package routy
Short Description A simple, robust PHP router for fast app and API development
License MIT
Homepage https://github.com/ginger-tek/routy
Informations about the package routy
Routy
A simple, robust PHP router for fast app and API development
Getting Started
Composer
Starter Example
Handlers for each route can be any kind of callable, such as regular functions, arrow functions, closure variables, or static class methods.
Configurations
You can pass an associative array of optional configurations to the constructor.
base
to set a global base URI when running from a sub-directorylayout
to set a default layout template file to use in therender()
reponse method
Features
Method Wrappers
Use the method wrappers for routing GET, POST, PUT, PATCH, or DELETE method requests. There is also a catch-all wrapper for matching on all standard HTTP methods, including HEAD and OPTIONS.
Use *
for the route argument to match on any route.
Custom Routing
You can also use the route()
method directly, which is what the common wrappers use underneath, to craft more specific route conditions on which to match.
Dynamic Routes
To define dynamic route parameters, use the :param
syntax and access them via the params
object on the $app
context.
Middleware
All arguments set after the URI string argument are considered middleware functions, including the route handler, so you can define as many as needed.
Use the native $_REQUEST
and $_SESSION
globals to share data between middleware/handlers.
Route Groups
You can define route groups using the group()
method.
You can also add middleware to your nested routes
Fallback Routes
Fallbacks are used for returning custom 404 responses, or to perform other logic before returning.
To set a fallback route, use the notFound()
method to set a handler function that will have the HTTP 404 response header already set.
Fallback routes are scoped to wherever they are defined, and will only be reached if they match the incoming URI's parent path.
Serve Static Files (SPA)
To serve static files from the base URI for a SPA frontend directly from the same app, use the serveStatic()
method after all the other route definitions.
This function is typically performed by your actual web server (Apache/nginx/caddy), so use with caution in production scenarios. Since this function enables your app to handle file serving, your application must run as a router for files to be served properly, i.e. php -S localhost:8000 router.php
.
If the requested file isn't found, the path + index.html
will be served instead. If there's no index.html
file, the failing path will be returned.
html
## Response Helper Methods
There are plenty of helper methods for handling responses.
### `sendJson()`
Use to return data as a JSON string
### `sendData()`
Use to return string data or a file's raw contents
### `redirect()`
Use to send a temporary or permanent redirect to a new URL
### `render()`
Use to render a PHP view file, using standard PHP includes and variable scope extraction for MCV modeling
You can set a default layout via the constructor config to use
You can also override the default by settings the `layout` option to another path
Or you can use no layout by setting the `layout` option to `false`
You may also not specify a layout at all, and just render files as is
Finally, set the `model` option to pass in a data model to expose to the template context in your view files. The current app instance is also exposed to the template context automatically
### `status()`
Use to set the HTTP status code. This can be used for method chaining to other response methods
### `end()`
Use to return immediately with an optional HTTP status code