Download the PHP package sformisano/jetrouter without Composer

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

JetRouter - WordPress Routing, Fast & Easy

Build Status Code Climate

This WordPress plugin adds a regular expression based router on top of the WordPress standard routing system. Any route declared through this router will take priority over standard WordPress URLs.

Installation

Download zip

Download via WordPress plugin page

You can visit the WordPress plugin page and download the latest plugin version there.

Download via GitHub repo release page

You can also grab the latest release from this repository's releases page.

Composer

If your setup uses Composer for proper dependency management (check out the awesome Bedrock project if you are interested in running WordPress as a 12 factor app) installing the JetRouter is quite easy.

Composer via WordPress Packagist (simpler & better)

WordPress Packagist is a site that scans the WordPress Subversion repository every hour for plugins and themes and then mirrors them as Composer repositories.

Search for "jetrouter" (here's the search results for that ), click on the latest version in the "Versions" column of the record and you will be given the code to paste in your composer.json file in the require property.

It should look something like this:

Please note: I have had no time to verify this, but even without using the Bedrock WordPress boilerplate, wpackagist packages should automatically install in the plugins directory (which is what you would want) rather than the default composer vendor directory. If that is not the case, reference the Composer via GitHub releases section below to learn how to tell composer where to install your WordPress plugins.

Composer via GitHub releases

Add this package to the "repositories" property of your composer json definition (make sure the release version is the one you want to install):

Once that's done, add the JetRouter in the composer require property (the version has to match the package version):

Finally, make sure to specify the correct path for the "wordpress-plugin" type we assigned to the JetRouter package in the definition above (note: if you are using Bedrock this is already taken care of).

Run composer update from your composer directory and you're good to go!

Usage

Initialization & Configuration

In your theme’s functions.php file or somewhere in your plugin:

Available router configuration properties:

The router automatically hooks itself to WordPress, so once you’ve created the router instance with the configuration that suits your needs, all that is left to do is adding routes. After that, the router is ready to dispatch requests.

Pro tip on the namespace

If you use the JetRouter to build a json/data api, or if you simply don’t mind having a namespace before your urls, having one will speed up standard WordPress requests, especially if you have many dynamic routes.

This is because, if the router has a namespace, but the http request’s path does not begin with that namespace (like it would be the case with all WordPress urls), the router won’t even try to dispatch the request.

Without a namespace, on the other hand, the router will try to dispatch all requests, building the dynamic routes array every time. (Note: caching the generated dynamic routes is on my todo list.)

Adding Routes

The basics

The basic method to add routes is addMethod. Example:

The addRoute method has aliases for each http method (get, post, put, patch, delete, head, options). Example:

Dynamic routes

Use curly braces to outline parameters, then declare them as arguments of the callback function and the router will take care of everything else. Example:

Parameters regular expressions

By default, all parameters are matched against a very permissive regular expression:

You can use your own regular expressions by adding a colon after the parameter name and the regex right after it. Example:

You can also use one of these regex shortcuts for convenience:

If, for example, we wanted to write the same get_school_students_by_year route by using the :i shortcut, this is how we would do it:

Optional parameters

You can make a parameter optional by adding a question mark after the closing curly brace. Example:

You can make all parameters optionals, even when they are not the last segment in a route. Example:

All the example requests below would match the route defined above:

This is not necessarily the best use of optional parameters. The examples here are just to show that, if you have a valid use case, the JetRouter will allow you to setup your routes this way.

Reverse Routing

Reverse routing is done through the same router object for simplicity’s sake.

Given this simple example route:

To print out the full path you use the thePath method:

You can also get the value returned, rather than printed out, by using the getThePath method (trying to follow some WordPress conventions with this):

Dynamic routes reverse routing

Dynamic routes work in the exact same way, you just pass the values of the parameters after the route name in the same order they are defined in when you created the route.

For example, given this route:

This is how you would print out a full path to this route:

As the last parameter is optional you can simply omit it:

If the optional parameter you need to omit is not the last parameter, you can simply pass null as a value and it will be ignored. Example:

I prefer this approach to the more common associative array for arguments as it makes everything simpler (less typing in the vast majority of scenarios) and more explicit (a missing optional parameter is still defined as null, so there’s a 1:1 match between formal parameters and arguments).

Handler's respond_to Feature

If you ever played around with Ruby on Rails, you're probably familiar with the respond_to and respond_with blocks. This respond_to feature is a basic, simplistic imitation of that clever way to handle different request types with different behaviours.

If you never tried out Rails, let me make this simple with an example:

You created a public signup form that posts to a create_user endpoint created with the JetRouter. You definitely want that form to work with ajax, but you're one of those few developers left who still cares about graceful degradation, so you don't just want to set the outputFormat config parameter to json and do a return $data; of sorts. You need to be able to receive json data with ajax requests, while doing something else (redirects, loading views in error mode etc.) for standard, synchronous requests.

Technically, you could manually check for $_SERVER['HTTP_X_REQUESTED_WITH'] and write some conditional code that works with that, but who wants to do that kind of manual labour for every single endpoint?

JetRouter's respond_to to the rescue! All you have to do is leave the outputFormat config parameter to auto and have your route handler return an array following this format:

Pro tip: appending ?json to a request path will force the json output.

Why I built the JetRouter

Credits

Go check both these authors and their projects, they are awesome.


All versions of jetrouter with dependencies

PHP Build Version
Package Version
No informations.
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 sformisano/jetrouter contains the following files

Loading the files please wait ....