Download the PHP package tnapf/router without Composer
On this page you can find all versions of the php package tnapf/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package router
Short Description Totally Not Another PHP Framework's router package
License MIT
Informations about the package router
Tnapf/Router
Totally Not Another PHP Framework's Route Component
Table of Contents
- Installation
- Routing
- Routing Shorthands
- Route Patterns
- Static Route Patterns
- Dynamic Placeholder-based Route Patterns
- Dynamic PCRE-based Route Patterns
- Controllers
- Responding to requests
- Catchable Routes
- Catching
- Specific URI's
- Middleware
- Postware
- Group routes
- Static Arguments
- React/Http Integration
Installation
Routing
You can manually create a route and then store it with the addRoute method
If you want the same controller to be used for multiple methods you can do the following...
Routing Shorthands
Shorthands for single request methods are provided
You can use this shorthand for a route that can be accessed using any method:
Route Patterns
Route Patterns can be static or dynamic:
- Static Route Patterns contain no dynamic parts and must match exactly against the
path
part of the current URL. - Dynamic Route Patterns contain dynamic parts that can vary per request. The varying parts are named subpatterns and are defined using either Perl-compatible regular expressions (PCRE) or by using placeholders
Static Route Patterns
A static route pattern is a regular string representing a URI. It will be compared directly against the path
part of the current URL.
Examples:
/about
/contact
Usage Examples:
Dynamic Placeholder-based Route Patterns
This type of Route Pattern is the same as Dynamic PCRE-based Route Patterns, but with one difference: they don't use regexes to do the pattern matching but they use the easy placeholders instead. Placeholders are strings surrounded by curly braces, e.g. {name}
.
Examples:
/movies/{id}
/profile/{username}
Placeholders are easier to use than PRCEs, but offer you less control as they internally get translated to a PRCE that matches any character (.*
).
Dynamic PCRE-based Route Patterns
This type of Route Pattern contains dynamic parts which can vary per request. The varying parts are named subpatterns and are defined using regular expressions.
Commonly used PCRE-based subpatterns within Dynamic Route Patterns are:
\d+
= One or more digits (0-9)\w+
= One or more word characters (a-z 0-9 _)[a-z0-9_-]+
= One or more word characters (a-z 0-9 _) and the dash (-).*
= Any character (including/
), zero or more[^/]+
= Any character but/
, one or more
Note: The PHP PCRE Cheat Sheet might come in handy.
The subpatterns defined in Dynamic PCRE-based Route Patterns are passed into the route's controller like dynamic placeholders.
]
Controllers
When defining a route you pass an instance of a class that implements Tnapf\Router\Interfaces\ControllerInterface
or a closure which will get converted into an instance of Tnapf\Handlers\ClosureRequestHandler
Responding to requests
All controllers MUST return an implementation of \Psr\Http\Message\ResponseInterface
. You can use the premade response object passed into the controller or instantiate your own. I recommend taking a look at HttpSoft/Response for prebuilt response types.
Catchable Routes
Catchable routes are routes that are only invoked when exceptions are thrown while handling a request. To create a catchable route you can do the following...
Catching
Note that $route->exception
will only be instantiated when catching.
Also note that catching \Throwable
will catch EVERY exception but catching \Exception
will only catch \Exception
Specific URI's
Note: Catchers are treated just like routes meaning they can have custom parameters
Middleware
Middleware is part of the request handling process that comes before the route controller is invoked.
A good example of middleware is making sure the user is an administrator before they go to a restricted page. You could do this in your routes controller for every admin page sure but, that would be redundant.
You can add middleware to a route by invoking the addMiddleware method and supply controller(s).
NOTE: The controllers will be invoked in the order they're appended!
*Another Note: If you don't want to proceed to the next part of request just return a ResponseInterface
instead of invoking RouteRunner::Next
Postware
Postware is a type of middleware that operates on the response generated by the Controller and can modify the response data before it is sent to the client. While it doesn't sit between the Controller and the View, it does operate on the response after the View has been generated.
Adding postware is just like middleware, just with a different method.
Group routes
If you have multiple routes to inherit the same base uri and the same before/after middleware then you can define them inside the group method...
Static Arguments
If you want to pass static arguments to your controller you can do so by using the addArgument
method on the Route object.
You would then be able to access the argument in your controller like any other argument. Note that all static arguments will override any arguments that are passed in the URI.
React/Http Integration
All versions of router with dependencies
php Version >=8.1
commandstring/utils Version ^1.4
httpsoft/http-emitter Version ^1.0
httpsoft/http-response Version ^1.0
httpsoft/http-message Version ^1.0.12