Download the PHP package tbolner/flex-php-router without Composer

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

FlexPhpRouter

Simple and flexible router library for web and command line applications.

Packagist page: https://packagist.org/packages/tbolner/flex-php-router

Recommended IDE: PhpStorm

Installation

Include the library in the composer.json file of your project:

Then execute:

composer update

This library requires PHP 7.1 or newer.

Usage example for web applications

Create a folder in your project, to contain the controllers. For example:

project1/src/Controller/

Then create the controllers. The file name has to match the first part of the URI:

Example URL Controller path
GET http://host/items/12 project1/src/Controller/items.php
POST http://host/items/12/list project1/src/Controller/items.php
PUT https://host/items/12/detailsasd project1/src/Controller/items.php
DELETE http://host/test%25Ctr4/as%25d/66 project1/src/Controller/testCtr4.php
GET http://host/test+_Ctr%255/qwerty/66 project1/src/Controller/test_Ctr5.php
GET http://host/test-Ctr6/qwerty/66 project1/src/Controller/test-Ctr6.php

As you see all special characters are removed from the first part of the URI when looking up the corresponding PHP file. (For security considerations.) To be precise: all characters are removed from the first part, which are not: a-z, A-Z, 0-9, - (dash), _ (underline). (Other parts of the URL are unaffected by this mechanism.)

The controller will match for patters to decide what to execute. These matching parts are called "actions".

project1/src/Controller/items.php

Notes:

Finally add the router start-up code to your application:

project1/web/index.php

Notes:

Path-dependent authentication and more

You can pass an optional callback as the fourth parameter of Router::route():

It will be executed before any controller code, with the knowledge of the selected controller path. The callback receives 2 parameters. The first '$isWeb' is true when a web controller gets executed and false when a CLI. The second tells which path was selected.

This feature allows you to implement authentication for a selection of API entries. The recommendation is that in case of an auth failure, throw an exception created by you, for example: MyAuthException, and handle it at the main entry point, where normally all exceptions are caught.

Features

API path prefix

The 3rd parameter of Router::route(...) specifies an API prefix, which will always get ignored during the routing process, like if it was cut off from the beginning of the URI.

Example:

Router::route(dirname(__FILE__)."/../src/Controller", 'default', 'api');
Example URL Controller path
http://host/api/items/12 project1/src/Controller/items.php
http://host/api/other/list project1/src/Controller/other.php

Notes:

Default page / 404 page

The default controller (described earlier) is called in two cases:

As a result we can always put an action at the end of the default, which is called when nothing got executed.

Controller/default.php

Notes:

Usage example for CLI applications

Create a folder in your project, to contain the CLI controllers. For example:

Then create the controllers. The file name has to match the first part of the path passed as first parameter:

Example console command Controller path
console.php test/cleanup project1/src/CLI/test.php
console.php test/run param1=55 param2="asd" project1/src/CLI/test.php
console.php other/do/something project1/src/CLI/other.php

Example controller:

project1/src/CLI/test.php

In this case the router start-up code has to go into a not web related PHP file (so it should be outside of the web folder). For example:

project1/console.php

Notes:

Design considerations


All versions of flex-php-router with dependencies

PHP Build Version
Package Version
Requires php Version >=7.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 tbolner/flex-php-router contains the following files

Loading the files please wait ....