Download the PHP package websk/php-simple-router without Composer
On this page you can find all versions of the php package websk/php-simple-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download websk/php-simple-router
More information about websk/php-simple-router
Files in websk/php-simple-router
Package php-simple-router
Short Description Simple routing and sitemap generation library for PHP applications
License MIT
Informations about the package php-simple-router
PHP Simple Router
A small library for routing HTTP requests in PHP applications and generating XML sitemaps.
The router checks the current path against regular expressions in declaration order, extracts parameters from captured groups, and passes them to a controller method. The library does not impose an application structure or require a dependency injection container or HTTP framework.
Features
- Map URLs to controller methods.
- Pass captured URL segments to method arguments after URL decoding.
- Group routes by a common pattern.
- Register a predefined set of routes for a CRUD controller.
- Resolve a URL handler from the command line.
- Collect URLs from controllers and generate an XML sitemap.
- Split a large sitemap across multiple files.
- Remove expired sitemap files.
Requirements
- PHP 8.3 or later.
- The
ext-xmlwriterextension for sitemap generation.
Installation
Basic routing
A controller can be passed as either an object or a class name. When a class name is used, the router creates the object without constructor arguments.
For a request to /articles/hello%20world?draft=1, the show() method receives hello world. The query string is ignored during route matching.
After a matching handler runs, the router terminates the PHP script. To continue checking subsequent routes, the controller method must return SimpleRouter::CONTINUE_ROUTING:
Declare routes from the most specific to the most general. The application can handle an unmatched request after the route declarations.
Route groups
matchGroup() lets the application skip a block of route declarations when the current URL does not match a common pattern:
Static routes
staticRoute() creates a controller, stores it as the current controller, and calls the specified method. Captured parameters are URL-decoded. An optional layout file is appended as the final method argument.
The active controller is available through SimpleRouter::getCurrentControllerObj(). If the handler returns SimpleRouter::CONTINUE_ROUTING, the router clears the current controller and continues execution.
CRUD routes
The routeBasedCrud() method registers a standard set of URLs for a controller:
| URL | Controller method |
|---|---|
/items |
listAction() |
/items/add |
addAction() |
/items/create |
createAction() |
/items/edit/{id} |
editAction($id) |
/items/save/{id} |
saveAction($id) |
/items/delete/{id} |
deleteAction($id) |
The controller must provide the public methods shown above and have no required constructor arguments.
Resolving a URL handler from the CLI
Command-line utilities can provide the URL explicitly:
When a route matches, its handler is not called. Instead, the router throws an Exception whose message contains the controller class, method, and captured arguments:
Sitemap generation
The sitemap generator uses configuration provided by websk/php-config:
static_data_pathis an existing directory for public static files.sitemap.rootis the sitemap directory relative tostatic_data_path.site_domainis the domain used in the sitemap index.
Controllers that provide sitemap URLs implement InterfaceSitemapController:
The url field is required. When freq is omitted, it defaults to never.
To build the sitemap, set the builder before loading the application's regular route declarations, then call finish():
In sitemap mode, route handlers are not called. Each controller class is processed once, and its URLs are collected through getUrlsForSitemap(). Controllers must be instantiable without constructor arguments.
SitemapBuilder writes up to 2,500 URLs per XML file and creates a sitemap.xml index. Old build directories can be removed with:
This removes sitemap directories older than two days.
Development
This command runs PHP syntax checks and the PHPUnit test suite.
License
MIT. See LICENSE for the full license text.
All versions of php-simple-router with dependencies
ext-xmlwriter Version *
websk/php-utils Version 3.0.*
websk/php-config Version 2.2.*