Download the PHP package webiny/router without Composer

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

Router component is used for mapping defined paths/urls to appropriate controllers or services.

Install the component

The best way to install the component is using Composer.

For additional versions of the package, visit the Packagist page.

Usage

Defining a route is a rather easy process, you set a route name and underneath you define the path and the callback. Here is an example:

If a route is matched you will get an instance of MatchedRoute. The getCallback method returns the value of callback parameter of the matched route. The second method getParams returns the values of the parameters defined in the Path section.

By registering a default Router configuration, Router will automatically set the defined routes and cache driver.

Later on you can use the RouterTrait access that Router instance.

NOTICE: When defining the routes in your config file, make sure that you put routes with lesser precision to the bottom, and routes with a more stricter rules at the top. A common pitfall is that a route with more wider pattern is matched before a route with a more stricter rules due to the fact that the Router loops over the defined routes in the same order that they are defined inside the config file, and it stops once the first route is matched.

Options

The Options attribute under the route provides you two additional options that you can set under each route.

Pattern

The default regex pattern for matching a variable is {[\w-]+}. Using the Pattern attribute you can set your own rule.

Default

The Default attribute gives you the option to set the the default value for a variable.

If the route is not matched using the current available properties from the url path, Router will replace all the variable patterns with the default values and try to match the route again.

Matching routes

To check if a route matches the given path, use the RouterTrait and then just call $this->router()->match() method. The match method takes either a string that is representing a url or it can take an instance of Url standard object. If the router successfully matches one of the routes it will return an array with callback and params attributes. If the router doesn't match any of the routes, it will return false.

NOTE: Router component always returns the first route that matches the given path.

Executing route callback

If you define your callback as string, you will have to parse and execute it on your own. But if you follow the standard structure of your callback you will be able to use router's execute method and pass your MatchedRoute to it. Router will then execute the callback for you and do all the checks regarding class and method existence:

If for some reason you need to call the method statically, define your route callback like this:

Generating routes

With the Router your don't have write the urls inside your code or views, instead you can generate the urls from the given route like this:

You see that Router replaced the {tag} parameter with the provided value, in this case it was html5. You can also notice that the page parameter isn't defined in our route, so the Router appended that parameter as a query string.

Configuration

The Router component takes one additional configuration parameter, and that is the Cache parameter. If either defines the name of the cache service that will be used to cache some of the internal mechanisms, or you can set it to false if you don't want the component to use cache.

Using the cache can speed up matching of routes, especially more complex ones.

Resources

To run unit tests, you need to use the following command:

$ cd path/to/Webiny/Component/Router/
$ composer.phar install
$ phpunit

All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version ^7
webiny/std-lib Version ~1.6
webiny/cache Version ~1.6
webiny/config Version ~1.6
webiny/http Version ~1.6
webiny/service-manager Version ~1.6
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 webiny/router contains the following files

Loading the files please wait ....