Download the PHP package maer/router without Composer

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

A small and simple PHP router

Build Status

A small, simple, extendable one-file PHP router with groups, filters and named routes

I'm not claiming that this router is faster or better than other routers out there. It's kind of hard to beat something like nikic/FastRoute. The two main reasons for building this was: 1. I wanted a simple but yet flexible, plug and play router with minimal to none setup. 2. It's fun to build stuff and you learn a lot from it!

Usage

Install

Clone this repository or use composer to download the library with the following command:

Change dev-master to the last tagged release.

Simple example

Route parameters

There are some placeholders you can use for route parameters. All parameters will be passed along to the controller in the same order as they are defined in the route:

Route callbacks

Route callbacks can be defined in different ways:

All callbacks will receive any route parameter.

If you send in a class method (non static), the router will instantiate the class and then call the method, when the router is dispatched and a match is found.

Filters

There are before and after filters:

Filter callbacks can be in the same formats as Route callbacks, meaning that you can use class methods as filters.

The before filter will receive all route parameter, just like the route callback. The after filter will also receive all parameters, but the first parameter will be be the response from the route callback.

Note: Filters will be called in the same order as they were defined. If any filter returns anything other than null, the dispatch will stop and that response will be returned instead.

Named routes

Add a name to any route

If you don't pass enough arguments to cover all required parameters, an exception will be thrown.

Prepend base url

By default, the router returns paths without the base url (protocol + hostname)

However, if you want the response from getRoute() to include the base url, you need to set the base url:

When you now want to get a named route, you can pass a boolean as third argument. true = include base url and false won't.

If you don't want to pass a third argument, but rather have it returning the base url as default, you can set that using:

If you call getRoute() without the third argument, it will always prepend the base url unless you pass false as third argument to getRoute().

Grouping routes

Instead of adding the same filters over and over for many routes, it's easier to group them together.

The $r->group()-method only takes an anonymous function as callback. The router instance is always passed as an argument to the callback.

When defining a group, you can add before and after filters, just like you do for routes. You can also use a prefix for a group, as described below.

Group Prefix

To add the same prefix to a group, use the prefix argument.

You can mix before, after and prefix when creating groups.

CRUD routes

To simplify the creation of CRUD routes, there's a crud()-function.

The above is the same as if you would define the following routes:

You can of course use the crud() function inside a group as well.

Redirects

You can register the router to redirect certain URL's as well.

Note: All redirect routes are triggered before any other registered routes.

Register a redirect:

You can also register before-filters on a redirect.

Redirect current request to a named route

When you register i redirect using the above method, it will be handled on dispatch, just like any other route.

Sometimes you want to redirect the user straight away, maybe after the router already has been dispatched (or even before). You can use toRoute() for that:

The above will redirect the request immediately. It's just like doing header('location: ...').

Dispatch the router

To dispatch the router, it's usually enough to just call the $r->dispatch()-method. How ever, if you want to dispatch the router with some specific URL and method you can pass them to the dispatcher (this is useful if you're writing tests):

If you rather trigger all the callbacks (filters and route callbacks) yourself, if you, for example, are using an IoC container, call the $r->getMatch() method instead and you will get the matched route object back.

If the before and after filters are closures, you can trigger them via:

Not found

If there is no match, a Maer\Router\NotFoundException will be thrown. You can register a callback that will be executed instead, using the $router->notFound()-method:

Method not allowed

If there is a url match but with the wrong http verb, a Maer\Router\MethodNotAllowedException will be thrown. You can register a callback that will be executed instead, using the $router->methodNotAllowed()-method:

Adding a custom callback resolver

If your callback is in the format of ['Classname', 'method'], you might want to customize how it's resolved. This is handy if you, for example, are using some kind of IoC with dependency injection.

To create your custom resolver, use the $r->resolver()-method. Example:


Release notes

1.5.0

1.4.0


If you have any questions, suggestions or issues, let me know!

Happy coding!


All versions of router with dependencies

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

Loading the files please wait ....