Download the PHP package weew/router without Composer

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

Simple router

Build Status Code Quality Test Coverage Version Licence

Table of contents

Installation

composer require weew/router

Introduction

What the router basically does is matching a URL to a list of registered routes and returns you a route upon a successful match. If there was no match,you'll get null. A route can contain any value you want, since it's up to you to create a response based on the route after all. This gives you the flexibility to use the router together with any other existing dependency injection containers or any other components. The router doesn't do anything but matching a URL to a route.

Registering routes

Below you'll see the basic methods for route registration. Route path may contain some placeholders for expected values, like {id}. If the placeholder ends with a ?, like here {alias?}, it is considered optional. As the second argument you may pass anything you want. You'll have access to that value later, so you can create a response or similar.

Mostly you are going to use this definition format for your route definitions:

As you see in this example, you've got to write the SampleController class over and over again. You can avoid this by setting a controller class on the router itself. Doing so, will create a new nested router. Example below does exactly the same as the example above, except you have less boilerplate code.

Route parameters

Let's say you've defined some routes that expect a parameter to be set in the url, here you'll see how you can access them.

Matching routes

By now you have registered all of your routes and want to find the one that matches the specified url.

Custom patterns

In some situations you wont to be able to specify custom patterns for route parameters. For example, lets say you want your ids to consist of numerical characters only.

Firewalls

It is very easy to protect routes with custom filters. In general, if a single filter fails (returns false or throws an exception), no further filters will be called. Same goes the other way around. If a filter explicitly says that everything is ok (returns true), no further filters will be called.

A filter has to return a boolean value to indicate whether the affected routes are good to go or rather should be ignored. Filter work best with groups, see below.

Sometimes you might want to throw an exception that would hold the reason why a filter did not pass. If you simply throw a regular exception, this would kill the program flow, and even if you catch this exception somewhere, it would kill the whole routing process. Even though a particular route did not match, because a filter failed, there might be another one that would fit. After a regular exception was thrown, there is no way another route might match. To work around this you might simply wrap you exception in the FilterException. This would ensure that the routing process finishes as supposed to and gives a chance for another route to match. If no route was found after all, you original exception will be thrown.

Now, failure of a filter will not break the routing process. If a route gets matched after all, there will be no exception thrown. But if there was no other route that could take it's place (be matched instead), the UnauthorizedException will be thrown.

Parameter resolvers

Often you might want to process a route parameter and replace it with another one. For example when you're using models. This route /users/{id} would always hold the id of the requested user. Wouldn't it be cool if it would hold the user model instead?

User's id has been magically resolved to it's model. Now you can use it in your route handlers.

Rules

You might also want to specify additional routing restrictions based on the current url. For example, limiting your routes to a subdomain or protocol.

There are many other restrictions that you might find useful.

Grouping routes

Sometimes you have an obvious boundary between your routes. Lets say you want your api routes to be available from the api subdomain and over https only. All the other routes should remain as is.

Complete example

A complete example of how you might use the router out of the box. The router itself is very flexible and at the end it comes down to your preference on how you will use it. Basically all it does is returning a route. After that it's up to you how you want to handle it. You might dynamically resolve the controller, or event combine it with a dependency injection container.

Extensions

There are several extensions available:


All versions of router with dependencies

PHP Build Version
Package Version
Requires weew/http Version ^1.11
weew/helpers-array Version ^1.0.0
weew/helpers-string Version ^1.0.0
weew/url Version ^2.0
weew/contracts Version ^1.1
weew/url-matcher 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 weew/router contains the following files

Loading the files please wait ....