Download the PHP package crysalead/router without Composer

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

Router - HTTP Request Router

Build Status Code Coverage

Complete benchmark results can be found here.

Installation

API

Route patterns

Route pattern are path string with curly brace placeholders. Possible placeholder format are:

Variable placeholders may contain only word characters (latin letters, digits, and underscore) and must be unique within the pattern. For placeholders without an explicit regex, a variable placeholder matches any number of characters other than '/' (i.e [^/]+).

You can use square brackets (i.e []) to make parts of the pattern optional. For example /foo[bar] will match both /foo and /foobar. Optional parts can be nested and repeatable using the []* or []+ syntax. Example: /{controller}[/{action}[/{args}]*].

Examples:

Note: the difference between /{controller}[/{action}[/{args}]*] and /{controller}[/{action}[/{args:.*}]] for example is args will be an array using [/{args}]* while a unique "slashed" string using [/{args:.*}].

The Router

The Router instance can be instantiated so:

Optionally, if your project lives in a sub-folder of your web root you'll need to set a base path using basePath(). This base path will be ignored so your routes won't need to be prefixed with it to matches the request path.

Note: If you are using the crysalead/net library you can pass Request::ingoing()->basePath(); directly so you won't need to set it manually.

The Router Public Methods

Route definition

Example of routes definition:

In the above example a route is registered using the ->bind() method and takes as parametters a route pattern, an optionnal options array and the callback handler.

The second parameter is an $options array where possible values are:

The last parameter is the callback handler which contain the dispatching logic to execute when a route matches the request. The callback handler is the called with the matched route as first parameter and the response object as second parameter:

The Route Public Attributes

The Route Mublic Methods

Named Routes And Reverse Routing

To be able to do some reverse routing, route must be named using the following syntax first:

Named routes can be retrieved using the array syntax on the router instance:

Once named, the reverse routing can be done using the ->link() method:

The ->link() method takes as first parameter the name of a route and as second parameter the route's arguments.

Grouping Routes

It's possible to apply a scope to a set of routes all together by grouping them into a dedicated group using the ->group() method.

The above example will be able to route /admin/user/edit on App\Admin\Controller\User::edit(). The fully-namespaced class name of the controller is built using the {controller} variable and it's then instanciated to process the request by running the {action} method.

Sub-Domain And/Or Prefix Routing

To supports some sub-domains routing, the easiest way is to group routes using the ->group() method and setting up the host constraint like so:

The above example will be able to route http://foo.hello.bar/admin/user/edit for example.

Middleware

Middleware functions are functions that have access to the request object, the response object, and the next middleware function in the application’s request-response cycle. Middleware functions provide the same level of control as aspects in AOP. It allows to:

And it's also possible to apply middleware functions globally on a single route or on a group of them. Adding a middleware to a Route is done using the ->apply() method:

You can also attach middlewares on groups.

Dispatching

Dispatching is the outermost layer of the framework, responsible for both receiving the initial HTTP request and sending back a response at the end of the request's life cycle.

This step has the responsibility to loads and instantiates the correct controller, resource or class to build a response. Since all this logic depends on the application architecture, the dispatching has been splitted in two steps for being as flexible as possible.

Dispatching A Request

The URL dispatching is done in two steps. First the ->route() method is called on the router instance to find a route matching the URL. The route accepts as arguments:

The ->route() method returns a route (or a "not found" route), then the ->dispatch() method will execute the dispatching logic contained in the route handler (or throwing an exception for non valid routes).

Dispatching A Request Using Some PSR-7 Compatible Request/Response

It also possible to use compatible Request/Response instance for the dispatching.

Handling dispatching failures

Setting up a custom dispatching strategy.

To use your own strategy you need to create it using the ->strategy() method.

Bellow an example of a RESTful strategy:

The strategy:

Acknowledgements


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version >=7
crysalead/collection Version ~3.0
crysalead/net Version dev-master
psr/http-message 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 crysalead/router contains the following files

Loading the files please wait ....