Download the PHP package devsrealm/tonics-router-system without Composer
On this page you can find all versions of the php package devsrealm/tonics-router-system. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devsrealm/tonics-router-system
More information about devsrealm/tonics-router-system
Files in devsrealm/tonics-router-system
Package tonics-router-system
Short Description An Imaginary PHP Router System For Tonics Projects
License Apache-2.0
Informations about the package tonics-router-system
A Trie based PHP Router System For Tonics Projects.
This would serve as a base router for tonics web apps, the router is different from most PHP Router in the sense that it doesn't use regex for matching urls, it instead uses a tree data structure where every path is hierarchically organized making it faster for finding both static or dynamic url.
Additionally, I came up with a concept called Node Teleporting which can further enhance and speed up searching dynamic routes, in the best case, dynamic route would be matched directly just like the static routes, and in the worse case, it would teleport a couple of times which is also faster than mere traversing.
You can learn more about the teleporting in the part 2 of how the router works.
Requirements
- PHP 8.0 and above
- PHP mbstring extension enabled.
Installation
If you don't want to use composer, go-to the release section and download the zip file that has a postfix of composer-no-required
e.g tonics-router-system-v1.0.0-composer-no-required.zip
Unzip it and require it like so:
How The Router Works
- A Faster Router System in PHP (Part 1)
- A Faster Router System in PHP (Part 2) (Improvement & Benchmarks)
Documentation
Before you get started, wire up the Router dependencies:
Basic routing
First parameter is the url paths which you want the route to match, and the second parameter could be a closure or a callback function that the route would call once the route matches.
If you want to keep things organized, you can also resolve through a class method, like so:
Request Interceptors
Some call it middleware, requestInterceptor
sounds plain and simple to me.
RequestInterceptors can be used to intercept a request before it moves to the next life cycle or to other request interceptors.
For example, if you have an admin url path: /admin, and you want to check if a user is logged in before processing the request, you use the request interceptor. Let's see an example:
in isAuthenticated()
class you can have something as such:
We implemented the TonicsRouterRequestInterceptorInterface
(it is a must to implement the interface to use the request interceptor) which provides a handle method with the $request object.
Inside the handle method, I am checking if user is not authenticated, and thus redirecting them to their proper destination.
However, if user is authenticated, the interceptor would move to the next life cycle in the route state, the next life cycle could be a new request interceptor or a class method or a callback delegation.
To add more request interceptors, simply do:
Route Required parameters
To match a dynamic url parameter you do:
where you capture the slug from the url, for example, if user visits /posts/blog-post-title, you get access
to blog-post-title
.
Alternatively you can do
where `PostsController could look like:
Route Groups
With the route group you could organize route in a tree like fashion, the good thing about this approach is
you can share route attributes, such as route interceptors, parent url paths, etc. across a large number of routes without needing to define those attributes on each individual route.
instead of doing this:
do this:
The end goal is identical to the above one but this is better organized.
You could also nest a group:
Route HTTP Verbs
$route->get(string $url, array|Closure $callback, array $requestInterceptor = [])
$route->post(string $url, array|Closure $callback, array $requestInterceptor = [])
$route->put(string $url, array|Closure $callback, array $requestInterceptor = [])
$route->patch(string $url, array|Closure $callback, array $requestInterceptor = [])
$route->delete(string $url, array|Closure $callback, array $requestInterceptor = [])
$route->match(array $method, string $url, \Closure|array $callback, array $requestInterceptor = [])
With match, you can match multiple HTTP verbs in one fell swoop.
More Documentation...
All versions of tonics-router-system with dependencies
php Version ^8.0