Download the PHP package rgjoni/router without Composer

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

license release last-commit packagist

Router

This is a small routing package with a focus in separation of concern and logical modularity while keeping the implementation simple enough.

Starting from the need of having an HTTP router to use with my API's/projects, this project quickly turned into a big learning experience on OO and the HTTP specification.

Table of contents

  1. Routing
  2. Static routing, workflow
  3. Dynamic routing
  4. Subrouting(groups)
  5. Handling
  6. HEAD requests
  7. Errors
  8. Acknowledgements
  9. Contributing

Installation

Namespace

Dependencies

none

Prerequisites

Supported methods

GET, HEAD, POST, PUT, PATCH, DELETE

Routing

The library offers static and dynamic routing(using placeholders), as well as subrouting(route groups) while also utilizing the annotation and callbacks as the handler.

Controller mapping, custom 404 callbacks & json error handling are also included in the package.

Setting controller mapping

In the case of using the "Controller@method" annotation, there is a default namespace mapping set to (of which there may only be one). This can be overridden (as the first thing in the workflow) if you want to use your own.

Static routing & usage workflow

That's about it as far as the usage workflow goes- call the Router with the desired method, path & handler and run it.

Homepage

Dynamic routing

The arguments MUST be enclosed by curly brackets() and match the following regular expression

Usage:

The argument name(in this case id) does not have to match the one in the handler.

Multiple arguments example:

In the case of a route with multiple arguments, the argument names MUST NOT conflict one another, this is because of how the parser works- it creates an array key for each argument(using its name).

Subrouting

When needed, bundling routes together for specific sections is easily implemented.

Subroutes MUST be declared using the group method and placed inside a callback function- the below example will build the expected routes(eg. "/admin/settings").

The group name MUST be preceded by a slash(/), as well as the subroutes(in this example etc).

Nested subrouting is also possible, the groups will then just append to each other().

Handling

As mentioned above, this router supports two types of handling:

The first part of the handler(before @) MUST only include the controller name which in turn MUST reside in namespace (see namespace section).

Depending on how you handled your namespace, an instance of the controller will be created and the method will be called, with or without the arguments.

If no method was passed or a typo was made regarding the separator @ (eg ) an exception is thrown and handled; another one is thrown if the class is not found in the namespace.

HEAD requests

As the RFC2616 HTTP specification implies, the router will redirect HEAD requests to identical, matching GET routes.

Error handling

The errors are all handled and returned in JSON form and are the value of an key

404 - Not found

If for some reason the router cannot find a route in the declaration, a default 404 error will be thrown:

Custom 404's

If wanting to handle your own 404 errors, you may do so using the method and a closure:

400 - Not allowed characters

If the URL matches the following pattern: an error "Not accepted characters in URL" is thrown.

405 - Method not allowed

In the case of a route having been found but the request not corresponding, a MethodNotAllowedException is thrown:

Acknowledgements

As I started with this project I knew next to nothing about routing and how would I go about solving this problem.

Only after having read the code of the following libraries, thinking about the patterns at play there and experimenting myself was I able to understand enough and find a solution that fit my needs and satisfied me.

Contributing

Pull requests and issues are welcome, please refer to CONTRIBUTING.md

License

MIT


All versions of router with dependencies

PHP Build Version
Package Version
Requires ext-json Version *
ext-http Version *
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 rgjoni/router contains the following files

Loading the files please wait ....