Download the PHP package triplepoint/groundhog-router without Composer
On this page you can find all versions of the php package triplepoint/groundhog-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package groundhog-router
Groundhog Router
Introduction
This library provides a request router which can interpret incoming requests, determine to what class the request maps, and return the action handler class ready for execution.
Dependencies are kept to zero, with interfaces provided for extension points.
Disclaimer
While I take great care to produce code that is free of excess dependencies and short-sighted assumptions, I feel I should warn you that this code is not primarily meant for public consumption. In other words, please understand that if you want to use this code in your own work you're free to do so under the provided license, but I'm not promising that the API will be stable or that the code will necessarily meet your needs out of the box.
So please, feel free to fork this code and adapt it to your own needs - or even better, offer comments on how I could improve the general-purpose nature of the code. But also accept that at the end of the day, this really is just a starting place for your own work.
Basic Structure
There are 3 core components in this library: the Router, a Route Parser, and a Routing Table Store. There are several secondary elements that get passed around as messages or used as helpers: RequestInterface, Route, etc. Finally, the end result is some object that implements RouteHandlerInterface.
Router
The Router takes in an object that represents the incoming request and which implements RequestInterface. This request is then delegated to the Routing Table Store in an attempt to find a matching Route. Once the Route is found, the Router constructs the appropriate object that implements RouteHandlerInterface, which is a Controller in typical framework terminology.
Route Parser
A Route Parser is any object that implements RouteParserInterface. It's responsibility is to acquire the set of Routes to which the project can respond. This is intentionally abstract - the included RouteParserAnnotation Route Parser operates on a set of phpdoc attributes to determine the Routes, but any strategy of route encoding could be used, with an appropriate Route Parser written to interpret it.
Routing Table Store
A Routing Table Store implements RoutingTableStoreInterface, and represents a cache in which to store the routing table once the Route Parser generates the set of Routes. There are Routing Table Stores included to support APC and SQLite, and a special "NoCache" Store which does not cache at all and instead prompts the Route Parser to always regenerate the routing table. Alternative storage mechanisms can easily be added by implmenting new objects against RoutingTableStoreInterface.
RequestInterface
Objects that implement the RequestInterface represent the incoming request. There generally need only be one of these implemented, and in an attempt to remain independant of other libraries, it is left to the user to implement this object. The methods defined in RequestInterface are generally based on Symfony's Http-Foundation library, but anything that properly implements this interface is valid.
Route
This object represents a single route rule, and is used as a messenger container between the Router, Route Parser, and Routing Table Store.
RouteHandlerInterface
These objects are the traditional controllers in MVC architecture. In an attempt to contain dependencies while allowing for testing, these objects can announce their preferred dependency injection container which is then passed back to them for consumption. Also, these objects are loaded by the Router with any incoming request's call parameters that may be present.
Example
First, lets define some classes that must be implemented:
Now that these classes are defined, we can set up the router and use it.
In practice, a lot of the creation and configuration of the various dependencies can be moved into a depdency container, leaving a clean startup in index.php.