Download the PHP package sdidev/router without Composer

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

bramus/router

Build Status Source Version Downloads License

A lightweight and simple object oriented PHP Router. Built by Bram(us) Van Damme - http://www.bram.us

Features

Prerequisites/Requirements

Installation

Installation is possible using Composer

Demo

A demo is included in the demo subfolder. Serve it using your favorite web server, or using PHP 5.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 \Bramus\Router\Router, define some routes onto it, and run it.

Routing

Hook routes (a combination of one or more HTTP methods and a pattern) using $router->match(method(s), pattern, function):

bramus/router supports GET, POST, PUT, DELETE, and OPTIONS HTTP request methods. Pass in a single request method, or multiple request methods separated by |.

When a route matches, 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, an 'HTTP/1.1 404 Not Found' status code will be returned.

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.

Commonly used subpatterns within Dynamic Route Patterns are:

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

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

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

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

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:

Subrouting / Mounting Routes

Use $router->mount($baseroute, $fn) to mount a collection of routes onto a subroute pattern. The subroute pattern is prefixed onto 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->mount() in the callable that's already contained within a preceding $router->mount().

Custom 404

Override the default 404 handler using $router->set404(function);

The 404 will be executed when no route pattern was matched to the current URL.

SDI Implementation - Before Route Middlewares

The SDI fork of Bramus router will take an optional third argument in the route definition

The third method is the middleware to be run before the route funciton

Before Route Middlewares

bramus/router supports Before Route Middlewares, which are executed before the route handling is processed.

Like route handling functions, you hook a handling function to a combination of one or more HTTP request methods and a specific route pattern.

Unlike route handling functions, more than one before route middleware is executed when more than one route match is found.

Before Router Middlewares

Before route middlewares are route specific. Using a general route pattern (viz. all URLs), they can become Before Router Middlewares (in other projects sometimes referred to as before app middlewares) which are always executed, no matter what the requested URL is.

After Router Middleware / Run Callback

Run one (1) middleware function, name the After Router Middleware (in other projects sometimes referred to as after app middlewares) after the routing was processed. Just pass it along the $router->run() function. The run callback is route independent.

Note: If the route handling function has exit()ed the run callback won't be run.

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.

Integration with other libraries

Integrate other libraries with bramus/router 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:

A note on making HEAD requests

When making HEAD requests all output will be buffered to prevent any content trickling into the response body, as defined in RFC2616 (Hypertext Transfer Protocol -- HTTP/1.1): The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.

Unit Testing & Code Coverage

bramus/router ships with unit tests using PHPUnit.

Acknowledgements

bramus/router is inspired upon Klein, Ham, and JREAM/route . Whilst Klein provides lots of features it is not object oriented. Whilst Ham is Object Oriented, it's bad at separation of concerns as it also provides templating within the routing class. Whilst JREAM/route is a good starting point it is limited in what it does (only GET routes for example).

License

bramus/router is released under the MIT public license. See the enclosed LICENSE for details.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.0
peledies/rhonda Version ~1
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 sdidev/router contains the following files

Loading the files please wait ....