Download the PHP package rammewerk/router without Composer
On this page you can find all versions of the php package rammewerk/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rammewerk/router
More information about rammewerk/router
Files in rammewerk/router
Package router
Short Description Simple router for PHP
License MIT
Homepage https://rammewerk.com
Informations about the package router
Rammewerk Router
The Rammewerk Router is a PHP routing library. It lets you manage routes in your web app and includes features like class-based routing and custom parameter management. It's compatible with custom dependency injection systems too. It helps in creating dynamic and secure routing systems, making it easy to use for both new and experienced developers.
Installation
Install Rammewerk Router via composer:
Basic usage with class-based routing
In this class any path called, which is not starting with /blog/hello/
will be resolved to the index
method.
Closures
In its simple form you can define a route with a closure, like many other routers for PHP:
Handling response from route
You can also return value from route if you'd like to incorporate a response handler or similar.
Understanding the path handling
The Router has a distinctive approach to path handling that sets it apart from traditional PHP routers.
Route Mapping Mechanism
Consider a request made to the URL /product/item/123
. The router initiates its search by attempting to match the
entire route. If a defined route doesn't exist for /product/item/123
, it modifies its search strategy.
Sequential Trimming and Matching
The router starts trimming the path from the end and tries matching again. It first attempts to find a match
for /product/item
, then /product
, and finally /
if previous attempts were unsuccessful.
This is why an empty route /
is always required to be added before the router starts finding the path.
Parameter Capturing
Each trimmed segment from the path is added to the parameters list, in the sequence it appears in the requested path.
So, if the route /product/item
is a valid one, then 123
is captured as the first parameter.
Accessing Parameters in Code
To tap into these parameters, define a string
parameter within the callback or class method. Here is a PHP example for
better understanding:
In this scenario, 123
is passed to the $id
parameter when the route /product/item/123
is requested.
Handling Unmatched Routes
Remember, if a parameter like $id
is required but the request doesn't include it, the router will consider the route
unmatched. To circumvent this, simply make the parameter optional by setting a default value, as
shown: string $id = ''
. This way, even if the parameter is missing in the request, the route remains valid.
Configuring Routes
To add new routes, all you need to do is define the route along with its corresponding function or class. Here's how:
Note that you don't define any parameters in the route setup. Instead, the function or class method you provide decides what parameters it needs. When '/page' is visited, the function or method runs with the parameters it has specified.
Here's an example for adding a class-based route:
Handling Different Types of Requests
The router handles all web requests, no matter what type (like 'GET' or 'POST'). If you need it to react differently based on the request type, you can make a wrapper to do that. This helps you to fine-tune your app's actions for different web request types. Keep in mind that you need to know about web request types and how to design responses to use this feature.
Handling dependencies
You can define a register a custom way to handle dependencies by registering your dependency injection container:
Once registered it allows adding dependencies to other classes:
Note that the first given string parameter is the first extracted parameter from a given path.
Class-based routing
What makes this router unique, is its ability to define one or multiple classes that handle routes, based on its public methods.
It's offer a quick way to work with routing.
Let's define a simple Route class that handles all routes that starts with /product/
:
To register this class, we can define it so:
If we later on wants to add a new product route to handle product update, we could simply add a new method to our class:
Setting up a Custom Class Loader (Optional)
It's possible to customize how route classes handle their dependencies. This is done by using a class dependency loader which runs when a route class starts initializing.
This loader receives a ReflectionClass
object, mirroring the class being loaded. The router already created
this ReflectionClass
, saving you processing time. This feature is particularly useful in modular systems for
inspecting class namespaces and loading the right dependencies.
Here's how to register a custom loader:
In this example, $container->create($class->name);
creates an instance of the class.
Note: This is an optional feature. If you don't set up a custom class loader, the router will use its default
loader ($router->registerDependencyLoader
). So, your route classes will still load their dependencies, even without a
custom class loader.
Setting Up Route Authentication
For adding authentication checks before a route is accessed, you can define a default method. This method will run for every class-based route before it's loaded.
Here's how to register the default method:
Now, the router will run the 'hasRouteAccess' method before it loads any class-based route (note: this doesn't apply to
closure routes). This method should return true
or false
, indicating whether the route should be accessible.
Once you register the default authentication method, every class-based route must implement it. If a route class doesn't
need authentication, have its 'hasRouteAccess' method return true
, this will allow all requests to pass.
You can catch RouteAccessDenied
exceptions and handle them according to your application's needs. For example, you may
want to redirect the user to a login page or show an error message if access is approved.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Please note that this project is provided "as is" without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the project is with you.
Support
If you are having any issues, please let us know. Email at [email protected] or open an issue.