Download the PHP package robert430404/rc-router without Composer
On this page you can find all versions of the php package robert430404/rc-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robert430404/rc-router
More information about robert430404/rc-router
Files in robert430404/rc-router
Package rc-router
Short Description This is a simple router with regex parsing
License MIT
Informations about the package rc-router
What Is This?
This is RC Router. This is a simple regex based router that allows you to pass variables by using place holders in your route.
Why Write This?
I did it to flex my brain, and get a full understanding of how routing works in the PHP space. Rather than just reading about it and assuming I knew what did what, I wrote this to solidify my knowledge.
Installing The Package
Simply use composer:
composer require robert430404/rc-router
How Does It Work?
This is a composer package so it relies on composer for the autoloading of the classes. You then create a new instance of the Router() object and start assigning your routes to the instance. Once you have your routes defined, you then pass the Router() into the Resolver() and it handles your routes.
What Are Some Of The Features?
The router supports both string and integer url variables, and passes them back to you in an array. You can extend this further in any way you want. The router is very flexible in how you can use it.
You can either pass in a closure, or a named handler function to each route to control what happens when a route is matched.
When a route is not found, a RouteNotFoundException is thrown from the resolver and allows you to catch and then create your 404 handler.
You can also now write in your own custom parsers and pass them into the resolver to make things like Controller@Action calling a lot easier and cleaner to implement.
If you don't pass a cusom parser, it defaults to the built in parser.
How Do The Placeholders Work?
When there are placeholders in your route they are returned to you as an array that you can access in your handler.
You have access to string placeholders. When you define your routes, you use {} to define string placeholders like this:
You also have access to integer placeholders. When you define your route, you use {:i} to define integer placeholders like this:
Some Examples
Simple closure based route:
Simple route with a named handler:
Regex closure based route with variables:
Regex route with variables and external handler:
What Is In The Returned Values?
The "$mapped" variable returns a structure like this: