Download the PHP package jinexus-framework/jinexus-route without Composer
On this page you can find all versions of the php package jinexus-framework/jinexus-route. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jinexus-framework/jinexus-route
More information about jinexus-framework/jinexus-route
Files in jinexus-framework/jinexus-route
Package jinexus-route
Short Description Route component from JiNexus Framework
License BSD-3-Clause
Homepage https://github.com/jinexus-framework/jinexus-route
Informations about the package jinexus-route
JiNexus Route
JiNexus Route provides simple route matching and access to every registered route in an
application, together with a redirect helper, for jinexus-mvc applications.
A Route holds a name-keyed table of route definitions, derives the current request URI
from $_SERVER, and matches it against that table. A Redirect sends a Location header
for a named route and terminates the request.
- Documentation: https://framework.jinexus.com/documentation
- Issues: https://github.com/jinexus-framework/jinexus-route/issues
Requirements
- PHP
^8.5
Installation
Install via Composer:
Usage
Registering routes
A route table is an array keyed by the route name. Each entry needs a route key holding the
URI; any other keys are yours to use and are returned untouched on a match.
Matching the current request
A match returns the whole entry, keyed by its route name. Matching stops at the first hit, so if two names share a URI, the earlier one wins.
You can also match against a table passed in directly, without registering it. A supplied table takes precedence; an empty one falls back to the registered routes:
Resolving a route's URI by name
getRouteUri() follows the same rule as getMatchRoute(): a supplied $routes table wins,
and an empty one falls back to whatever setRoutes() registered. It throws a
RouteException when the name is found in neither.
Deriving the request URI
getUri() strips the directory the front controller lives in, drops the query string, and
normalizes the surrounding slashes:
SCRIPT_NAME |
REQUEST_URI |
getUri() |
|---|---|---|
/index.php |
/ |
/ |
/index.php |
/about-us/ |
/about-us |
/index.php |
/search?q=a/b |
/search |
/app/public/index.php |
/app/public/ |
/ |
/app/public/index.php |
/app/public/user/42 |
/user/42 |
This reads $_SERVER['SCRIPT_NAME'] and $_SERVER['REQUEST_URI'] directly, so both must
be present.
Redirecting
toRoute() throws a RouteException when the name is empty or unregistered and skips the
header if headers have already been sent. It terminates the request either way, so nothing
after the call runs.
The redirect a Route was constructed with is reachable from it:
Magic getters and setters
AbstractBase::__call() resolves getX()/setX() against public properties only.
Setters are fluent. Anything it cannot resolve — a protected property, an unknown property,
or a method that isn't a get/set prefix — throws a RouteException:
Error handling
Package-level failures throw JiNexus\Route\RouteException (a subclass of \Exception):
| Message | Thrown by |
|---|---|
Route "<name>" not found |
getRouteUri(), toRoute() |
Route name must be provided |
toRoute() with an empty name |
Not implemented: <class>::<property> |
AbstractBase::__call() |
Extending
Route and Redirect are deliberately empty subclasses of AbstractRoute and
AbstractRedirect. Keep them that way — the public accessors are implemented with PHP
property hooks over backing fields, and declaring a real property would shadow them. For
IDE autocompletion, prefer @property PHPDoc tags over real properties.
AbstractRedirect exposes three protected seams so the terminating redirect can be
tested: headersSent(), sendHeader(), and terminate(). Override them in a subclass to
observe or intercept the redirect instead of ending the request.
Testing
The suite is 52 tests at 100% line, method, and class coverage. phpunit.dist.xml is the
committed configuration. Use XDEBUG_MODE=off to silence the local Xdebug notice:
Contributing
Please see CONDUCT.md for the code of conduct. Contributions should include
tests and a CHANGELOG.md entry. See AGENTS.md for detailed build, style, and
workflow conventions.
License
BSD-3-Clause. See LICENSE.md.