blank-framework / file-path-router
Package for the Blank Framework FilePathRouter
Requires
Requires (Dev)
- nyholm/psr7: ^1.8
- pestphp/pest: ^2.24
- phpstan/phpstan: ^1.10
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2025-05-07 09:27:16 UTC
README
FilePathRouter is a simple routing package that maps a path such as /blog/posts
to the directory /blog/posts
. If
that directory exists, it executes the index.php
file inside.
Usage
To start, instantiate the FilePathRouter
class and pass the absolute path to the directory where your routes will be
located. If it is in the root of your project directory is could be like the code snippet below:
$router = new FilePathRouter(__DIR__ . '/routes');
To route your requests, get the requested path for example /blog and pass it into the function routeRequest as shown below:
$router->routeRequest('/blog');
Directory Structure and Routing
FilePathRouter's routing mechanism is dependent on your project's directory structure. The home route (/) is mapped to the index.php file in the /routes directory. To create a new path, such as /blog, simply create a directory named blog within /routes. Place an index.php file in this new directory, and this file will be executed whenever the /blog path is requested.
Exceptions
If the routes path you provided in the constructor does not exist, the router will throw a RoutesPathNotFoundException
.
If the requested route could not be found, the router will throw a RouteNotFoundException
.