Download the PHP package webshr/router without Composer

On this page you can find all versions of the php package webshr/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package router

Webshr Router

Latest Stable Version CI Coverage Status

A custom PHP Router for the WP HTMX WordPress plugin based on Rareloop Router.

Installation

Usage

Creating Routes

Map

Creating a route is done using the map function:

map() takes 3 parameters:

Route Parameters

Parameters can be defined on routes using the {keyName} syntax. When a route matches that contains parameters, an instance of the RouteParams object is passed to the action.

If you need to add constraints to a parameter you can pass a regular expression pattern to the where() function of the defined Route:

Optional route Parameters

Sometimes your route parameters needs to be optional, in this case you can add a ? after the parameter name:

Named Routes

Routes can be named so that their URL can be generated programatically:

If the route requires parameters you can be pass an associative array as a second parameter:

If a passed in parameter fails the regex constraint applied, a RouteParamFailedConstraintException will be thrown.

HTTP Verb Shortcuts

Typically you only need to allow one HTTP verb for a route, for these cases the following shortcuts can be used:

Setting the basepath

The router assumes you're working from the route of a domain. If this is not the case you can set the base path:

Controllers

If you'd rather use a class to group related route actions together you can pass a Controller String to map() instead of a closure. The string takes the format {name of class}@{name of method}. It is important that you use the complete namespace with the class name.

Example:

Creating Groups

It is common to group similar routes behind a common prefix. This can be achieved using Route Groups:

Middleware

PSR-15/7 Middleware can be added to both routes and groups.

Adding Middleware to a route

At it's simplest, adding Middleware to a route can be done by passing an object to the middleware() function:

Multiple middleware can be added by passing more params to the middleware() function:

Or alternatively, you can also pass an array of middleware:

Adding Middleware to a group

Middleware can also be added to a group. To do so you need to pass an array as the first parameter of the group() function instead of a string.

You can also pass an array of middleware if you need more than one:

Defining Middleware on Controllers

You can also apply Middleware on a Controller class too. In order to do this your Controller must extend the Webshr\Router\Controller base class.

Middleware is added by calling the middleware() function in your Controller's __constructor().

By default all Middleware added via a Controller will affect all methods on that class. To limit what methods Middleware applies to you can use only() and except():

Matching Routes to Requests

Once you have routes defined, you can attempt to match your current request against them using the match() function. match() accepts an instance of Symfony's Request and returns an instance of Symfony's Response:

Return values

If you return an instance of Response from your closure it will be sent back un-touched. If however you return something else, it will be wrapped in an instance of Response with your return value as the content.

Responsable objects

If you return an object from your closure that implements the Responsable interface, it's toResponse() object will be automatically called for you.

404

If no route matches the request, a Response object will be returned with it's status code set to 404;

Accessing current route

The currently matched Route can be retrieved by calling:

If no route matches or match() has not been called, null will be returned.

You can also access the name of the currently matched Route by calling:

If no route matches or match() has not been called or the matched route has no name, null will be returned.

Using with a Dependency Injection Container

The router can also be used with a PSR-11 compatible Container of your choosing. This allows you to type hint dependencies in your route closures or Controller methods.

To make use of a container, simply pass it as a parameter to the Router's constructor:

After which, your route closures and Controller methods will be automatically type hinted:

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Credits

License

MIT


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
altorouter/altorouter Version ^2.0
laminas/laminas-diactoros Version ^3.3
mindplay/middleman Version ^4.0
php-di/php-di Version ^7.0
psr/container Version ^2.0
psr/http-message Version ^2.0
psr/http-server-middleware Version ^1.0
spatie/macroable Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webshr/router contains the following files

Loading the files please wait ....