Download the PHP package commandstring/router without Composer

On this page you can find all versions of the php package commandstring/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

This is now abandonware!

If you still need a router in React/Http, checkout Tnapf/Router


CommandString/Router

A router package built that uses ReactPHP/HTTP under the hood

Table of Contents

Installation

Getting started

You first need to create a ReactPHP SocketServer

Then create a router instance

The second parameter is whether dev mode should be enabled or not you can read about dev mode here

create some routes then begin listening for requests

Routing

You can then add routes by using the match method

You can listen for more methods by adding them to the array

Routing Shorthands

Shorthands for single request methods are provided

You can use this shorthand for a route that can be accessed using any method:

Route Patterns

Route Patterns can be static or dynamic:

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:

Usage Examples:

Dynamic PCRE-based Route Patterns

This type of Route Pattern contains dynamic parts which can vary per request. The varying parts are named subpatterns and are defined using regular expressions.

Examples:

Commonly used PCRE-based subpatterns within Dynamic Route Patterns are:

Note: The PHP PCRE Cheat Sheet might come in handy.

The subpatterns defined in Dynamic PCRE-based Route Patterns are converted to parameters that are passed into the route handling function. The prerequisite is that these subpatterns need to be defined as parenthesized subpatterns, which means that they should be wrapped between parenthesis:

Note: The leading / at the very beginning of a route pattern is not mandatory, but is recommended.

When multiple subpatterns are defined, the resulting route handling parameters are passed into the route handling function in the order they are defined:

Dynamic Placeholder-based Route Patterns

This type of Route Pattern is 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 curly braces, e.g. {name}. You don't need to add parens around placeholders.

Examples:

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 does not need to match the name of the parameter that is passed into the route handling function:

Optional Route Subpatterns

Route subpatterns can be made optional by making the subpatterns optional by adding a ? after them. Think of blog URLs in the form of /blog(/year)(/month)(/day)(/slug):

The code snippet above responds to the URLs /blog, /blog/year, /blog/year/month, /blog/year/month/day, and /blog/year/month/day/slug.

Note: With optional parameters, it is important that the leading / of the subpatterns is put inside the subpattern itself. Don't forget to set default values for the optional parameters.

The code snipped above unfortunately also responds to URLs like /blog/foo and states that the overview needs to be shown - which is incorrect. Optional subpatterns can be made successive by extending the parenthesized subpatterns so that they contain the other optional subpatterns: The pattern should resemble /blog(/year(/month(/day(/slug)))) instead of the previous /blog(/year)(/month)(/day)(/slug):

Note: It is highly recommended to always define successive optional parameters.

To make things complete use quantifiers to require the correct amount of numbers in the URL:

Controllers

When defining a route you can either pass an anonymous function or an array that contains a class along with a static method to invoke. Additionally your controller must return an implementation of the PSR7 Response Interface

Anonymous Function Controller

Class Controller

I have a class with a static method, your handler MUST be a static method!

I then replace the anonymous function with an array the first item being the class string and the second key being the name of the static method.

404 Handler

Defining a 404 handler is required and is similar to creating a route. You can also have different 404 pages for different patterns.

To setup a 404 handler you can invoke the map404 method and insert a pattern for the first parameter and then your controller as the second.

500 handler

Defining a 500 handler is recommended and is exactly the same as mapping a 404 handler.

Note that when in development mode your 500 error handler will be overrode

Middleware

Middleware is software that connects the model and view in an MVC application, facilitating the communication and data flow between these two components while also providing a layer of abstraction, decoupling the model and view and allowing them to interact without needing to know the details of how the other component operates.

A good example is having before middleware that makes sure the user is an administrator before they go to a restricted page. You could do this in your routes controller for every admin page but that would be redundant. Or for after middleware, you may have a REST API that returns a JSON response. You can have after middleware to make sure the JSON response isn't malformed.

Before Middleware

You can define before middleware similar to a route by providing a method, pattern, and controller. Do note that with beforeMiddleware you're expected to create an object that implements ResponseInterface to pass to the main route.

In the main route for after middleware

If the router detects after middleware then the third parameter will be a closure with similar functionality to beforeMiddleware

After Middleware

Special note about middleware, you can pass variables from beforeMiddleware to the main route or from the main route to afterMiddleware by supplying it as the second argument in the next closure.

Template Engine Integration

You can use CommandString/Env to store your template engine object in a singleton. Then you can easily get it without trying to pass it around to your controller

Responding to requests

All controllers MUST return an implemantation of \Psr\Http\Message\ResponseInterface. You can use the premade response object passed into the controller or instantiate your own. I recommend taking a look at HttpSoft/Response for prebuilt response types. This is also included with the route as it's used for the dev mode

Running and Advance Usage

If you want more control over the http server you can use the getHttpServer method to create and return the HttpServer object

In addition to being able to retrieve the http server you can also retrieve the socket server with the getSocketServer method

Dev Mode

As of now dev mode does one thing. When an exception is thrown on your route it returns the exception with the stack trace as a response rather than dumping it into the console.

Nodemon

I would recommend using nodemon when developing as it will restart your server with every file change. To install nodemon you'll need nodejs and npm.

npm install -g nodemon

then in root of your project directory create a new file named nodemon.json and put the following contents into it

Afterwards instead of using php index.php to start your server use nodemon index.php and change a file. You'll see that it says the server is restarting due to a file change. And now you don't have to repeatedly restart the server when you change files! You can also enter r into the console to restart manually if needed!


All versions of router with dependencies

PHP Build Version
Package Version
Requires react/http Version ^1.8.0
twig/twig Version ^3.4
httpsoft/http-response Version ^1.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 commandstring/router contains the following files

Loading the files please wait ....