Download the PHP package wepesi/routing without Composer
On this page you can find all versions of the php package wepesi/routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wepesi/routing
More information about wepesi/routing
Files in wepesi/routing
Package routing
Short Description Simple and lightweight php module to write clean route of a web application
License Apache-2.0
Homepage https://github.com/bim-g/wepesi-router/issues
Informations about the package routing
wepesi/routing
A lightweight and simple object oriented PHP Router. Built by (Boss Ibrahim Mussa) and Contributors
Features
- Supports
GET,POST,PUT,DELETE,PATCHrequest methods - Routing shorthands such as
get(),post(),put(), … - Static Route Patterns
- Dynamic Route Patterns: Dynamic Placeholder-based Route Patterns
- Optional Route Subpatterns
- Subrouting / Group Routing
- Allowance of
Class@Methodcalls - Before Route Middlewares
- Before Router Middlewares / Before App Middlewares
- API Groupe Routing
- Supports
X-HTTP-Method-Overrideheader - Custom 404
- Works fine in subfolders
Prerequisites/Requirements
- PHP 7.4 or greater
- URL Rewriting
Installation
Installation is possible using Composer
Demo
A demo is included in the demo folder. Serve it using your favorite web server, or using PHP 7.4+'s built-in server by executing php -S localhost:8080 on the shell. A .htaccess for use with Apache is included.
Usage
Create an instance of \Wepesi\Routing\Router, define some routes onto it, and run it.
Routing
Wepesi/routing supports GET, POST, PUT, PATCH, DELETE HTTP request methods. Pass in a single request method.
When a route matches against the current URL (e.g. $_SERVER['REQUEST_URI']), the attached route handling function will be executed. The route handling function must be a callable. Only the first route matched will be handled. When no matching route is found, a 404 handler will be executed.
Routing Shorthands
Shorthands for single request methods are provided:
Note: Routes must be hooked before $router->run(); is being called.
Route Patterns
Route Patterns can be static or dynamic:
- Static Route Patterns contain no dynamic parts and must match exactly against the
pathpart of the current URL. - Dynamic Route Patterns contain dynamic parts that can vary per request. The varying parts are named subpatterns and are defined using either Perl-compatible regular expressions (PCRE) or by using placeholders
Static Route Patterns
A static route pattern is a regular string representing a URI. It will be compared directly against the path part of the current URL.
Examples:
/about/contact
Usage Examples:
Dynamic Placeholder-based Route Patterns
This type of Route Patterns are the same as Dynamic PCRE-based Route Patterns, but with one difference: they don't use regexes to do the pattern matching but they use the more easy placeholders instead. Placeholders are strings surrounded by collumn, e.g. :name.
Examples:
/movies/:id/profile/:username
Placeholders are easier to use than PRCEs, but offer you less control as they internally get translated to a PRCE that matches any character (.*).
Note: the name of the placeholder should match with the name of the parameter that is passed into the route handling function.
Dynamic PCRE-based Route Patterns
This type of Route Patterns contain dynamic parts which can vary per request. The varying parts are named subpatterns and are defined using regular expressions.
Usage Examples:
Commonly used PCRE-based subpatterns within Dynamic Route Patterns are:
\d+= One or more digits (0-9)\w+= One or more word characters (a-z 0-9 _)[a-z0-9_-]+= One or more word characters (a-z 0-9 _) and the dash (-).*= Any character (including/), zero or more[^/]+= Any character but/, one or more
When multiple subpatterns are defined, the resulting route handling parameters are passed into the route handling 'with` function in the order they are defined in:
This will be like a kind of validation of your parameters.
Subrouting / Groupe Routing
Use $router->group($baseroute, $fn) to group a collection of routes in to a subroute pattern. The subroute pattern is prefixed into all following routes defined in the scope. e.g. Mounting a callback $fn onto /movies will prefix /movies onto all following routes.
Nesting of subroutes is possible, just define a second $router->group() in the callable that's already contained within a preceding $router->group().
Class#Method calls
We can route to the class action like so:
or
When a request matches the specified route URI, the get_users method on the UserController class will be executed. The defined route parameters will be passed to the class method.
The method can be static(not-recommend) or non-static (recommend). In case of a non-static method, a new instance of the class will be created.
Note: In case you are using static method, dont pass as string or in array.
Before Route Middleware
wepesi/routing supports Before Route Middlewares, which are executed before the route handling is processed.
Route middlewares are route specific, one or middleware can be set and will be executed before route function.
API Groupe Routing
You can define your API route inside the api method, and will auto complet api route fro you.
Overriding the request method
Use X-HTTP-Method-Override to override the HTTP Request Method. Only works when the original Request Method is POST. Allowed values for X-HTTP-Method-Override are PUT, DELETE, or PATCH.
Custom 404
The default 404 handler sets a 404 status code and exits. You can override this default 404 handler by using $router->set404(callable);
You can also define multiple custom routes e.x. you want to define an /api route, you can print a custom 404 page:
Also supported are Class@Method callables:
The 404 handler will be executed when no route pattern was matched to the current URL.
💡 You can also manually trigger the 404 handler by calling $router->trigger404()
Integration with other libraries
Integrate other libraries with wepesi/routing by making good use of the use keyword to pass dependencies into the handling functions.
Given this structure it is still possible to manipulate the output from within the After Router Middleware
A note on working with PUT
There's no such thing as $_PUT in PHP. One must fake it:
Subfolder support
License
wepesi/routing is released under the Apache-2.0 license. See the enclosed LICENSE for details.