Download the PHP package lucatume/wp-routing without Composer

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

WordPress Wrappers

Deprecated: this package is no more maintained. See wp-routes for a possible alternative.

A set of wrapping classes that should allow Laravel-like routing in WordPress thanks to WP Router plugin and some packages from Laravel.

Including the library in a theme or plugin

The library is meant to be used with Composer in a Composer-managed project and to include in the project the following line should be added to composer.json require entry

{
    "require": {
        "lucatume/wp-routing": "dev-master"
    }
} 

WPRouting_Route

Setting routes

While the inner workings of the wrapper classes will build on WP Router code and hence any example applies the library will wrap the methods required to set up routes in a Laravel-like interface to go from this

// file my-routes-plugin.php

add_action('wp_router_generate_routes', 'generateMyRoutes');

function generateMyRoutes(WP_Router $router)
{
    $router->add_route('wp-router-sample', array(
        'path' => '^wp_router/(.*?)$',
        'query_vars' => array(
            'sample_argument' => 1,
        ),
        'page_callback' => array(get_class(), 'sample_callback'),
        'page_arguments' => array('sample_argument'),
        'access_callback' => TRUE,
        'title' => 'WP Router Sample Page',
        'template' => array('sample-page.php', dirname(__FILE__).DIRECTORY_SEPARATOR.'sample-page.php')
    ));
}

to this

// file my-routes-plugin.php

WPRouting_Route::get('wp_router/{word}', function($word){
        echo "Hello $word";
    })->where('word', '.*?')
      ->withTitle('Wp Router Sample Page')
      ->withTemplate(array(
        'sample-page',
        dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sample-page.php'
        );

Route methods

The entry point of any route generation is always one of 4 methods:

each one of the methods above will take two arguments

The path can contain placeholders strings like {word} in the example above that will require a definition using the where method later in the fluent chain.
The second parameter can be a simple callback function or be an array containing:

Any filter needs to be registered using the filter static method.

Examples

Please note that path variables are passed in their appearance order to the page, access and title callback methods/functions.
I want to add a /posts page displaying the posts archive

WPRouting_Route::get('posts', function(){
    echo 'My post archive';
})->withTitle('Archive');

I wand to add a /secret-posts page accessible to admins alone

WPRouting_Route::filter('admin', function(){
        return current_user_can('activate_plugins');
    }) ;

WPRouting_Route::get('secret-posts', array('admin', function(){
        echo 'Secret posts';
    }))->withTitle('Secret page');

I want to add PUT and POST endpoints for editors to edit posts

WPRouting_Route::filter('editor', function($id){
        return current_user_can('edit_posts', $id);
    });

WPRouting_Route::pattern('id', '\d+');

WPRouting_Route::put('posts/{id}', array('editor', function($id){
        echo 'Doing some post updating';
    }));

WPRouting_Route::post('posts/{id}', array('editor', function($id){
        echo 'Adding a post';
    }));

WPRouting_PersistableRoute

An extension of the base WPRouting_Route class that allows persisting route meta information to the WordPress database.

Persisting a route meta information

The class defines a shouldBePersisted method accepting a boolean that will trigger route meta information when set to true; by default routes will not be persisted.
At a bare minimum a route must define a title and a path to be eligible for persistence; it's a fluent method to be used like

    // file my-routes-plugin.php

WPRouting_PersistableRoute::get('hello', function(){
        echo "Hello there";
    })->withTitle('Wp Router Sample Page')
      ->withTemplate(array(
        'sample-page',
        dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sample-page.php'
        )
      ->shouldBePersisted();

The shouldBePersisted method will default to true if no value is passed to it and hence

->shouldBePersisted();
->shouldBePersisted(true);

If the persistence of the route meta is set to true then any route argument not related to WP Router (see the WPRouting_Route::$WPRouterArgs variable) and not excluded from persistence (see WPRouting_PersistableRoute::$nonPersistingArgs variable) will be persisted as route meta information. All routes meta will be stored in a single option in WordPress database (see WPRouting_PersistableRoute::OPTION_ID constant for its value) in an array using the structure

[
    'route-one' :
        ['title' => 'Route One', 'permalink' => 'route-one', 'some-meta' => 'some meta value']
    'route-two' :
        ['title' => 'Route Two', 'permalink' => 'route-two', 'some-meta' => 'some meta value']
]

where 1st level array keys are the routes ids.

Hooking to alter the route meta information

Before route meta information is persisted to the database and after the argument pruning has been made the WPRouting_PersistableRoute class offers a filter hook (see WPRouting_PersistableRoute::ROUTE_PERSISTED_VALUES_FILTER for its tag) to alter the route meta before it's persisted to the database.
The arguments for the filter function are the route arguments and the route id, the filter will trigger one time for each route.


All versions of wp-routing with dependencies

PHP Build Version
Package Version
Requires php Version >=5.2.0
lucatume/tdd-helpers Version 2.*
lucatume/wp-utils Version 2.*
lucatume/wp-wrappers Version 2.*
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 lucatume/wp-routing contains the following files

Loading the files please wait ....