Routers - aPisC/phroper GitHub Wiki
Routers are inherited from Phroper/Router base class. In the background a priorized list of handler functions are stored, that are called in decreasing order. Each handler is able to call the next in the list if not able to handle the incoming request.
Run
Routers have a run
function, this will call the registered handlers with the parameters passed in the first argument. The parameters must contain the currently processed url and the request method. The second parameter is a function that is called, when no handler could serve the request.
Handler registering options
addHandler($handler, $priority=0)
Handler functions are registered with the addHandler
function. The handler can be a function, an another Router, or any other class, that has a run
method. The handler / run function will be called with 2 argument, the first is an associated array collecting the parameters from the url processing, the second is a function that will hall the next handler in the queue. If the next function is not called, the router assumes that the request is served, and finshes the execution.
add($url, $handler, $method='*', $prioroty=0)
This function will register a handler that tries to match the current request with the given url and request metod. Calls the handler function if the url is matching and call the next handler in the queue otherwise. The handler function will get the unprocessed part of the original url in the parameters.
If the url ends with /
, the matcher will accept all url-s starting with the patters.
Url matching examples:
null
: Match with empty url"/"
: Accept any url"get/:id"
: Accept urls that have exactly 2 parts, the first isget
. The second part will be available in parameters withid
key"get/::path/list"
: Accept urls that have at least 3 parts, first isget
, the last islist
, and the middle of the url will be accessible throug thepath
key in parrameters":controller/"
: Match the urls that vave at least 1 parameter, this will be available incontroller
parameter, and the remaining part of the url will be unprocessed and can be anything.
addServeFolder($expression, $folder, $priority = -1000)
Registers a handler that tries to find a file requested in the url parameter in the specified folder. If the requested file is a folder, tries to serve the index.html in the requested folder. (only GET method allowed)
addServeFile($expression, $fn, $priority = -1100)
This function will register a handler that will serve a specified fallback file for every unhandled request. (only GET method allowed)