Routing to callback functions in WordPress – 04

I’m undergoing a slow and code-first discover of Laravel while trying to integrate some of its power in WordPress. I’m doing it out of a desire for improvement and knowledge and do not bear with me love nor hate toward either frameworks (or whatever WordPress is defined).

Moved the thing to its own package

To allow me for a more flexible composition of packages I’ve moved WP Router plugin and Illuminate wrapping classes to their own package which is both on GitHub and on Packagist. The decision was born out of the fact that integrating Laravel components requires PHP 5.4 while all other packages will require version 5.3.

WP Router wrapping to begin with

It all began wrapping WP Router entry point

// 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' => '^hello/(.*?)$',
        'query_vars' => array(
            'name' => 1,
        'page_callback' => func($name){ echo "Hello $name"; },
        'page_arguments' => array('name'),
        'access_callback' => TRUE,
        'title' => 'Hello You',
        'template' => array('templates/page.php')

into something that explicitly mocks Laravel routing translating the code above into

Route::get('hello/{name}', function($name){

    echo "Hello $name";

})->where('name', '.*?')
->withTitle('Hello you')


While the above solution did work problems did arise when setting access callbacks due to the fact that, to spare the arguments specification part, all of the route path arguments were passed to the callback functions; that’s a plus when it comes to template rendering functions but did go against Laravel idea of filters.
The idea is to define some filters and then apply them alone or in batches to a request and that was not possible if the same filter had to be used in different route access callback functions continuosly having to match the order and cardinality of the filter parameters with the access callback arguments; long story short: I’ve integrated, with very little code on my side, Laravel Request class in the package to be able to rewrite the above route to

Route::get('hello/{name}', function(){

    $name = Request::segment(1);
    echo "Hello $name";

})->where('name', '.*?')
->withTitle('Hello you')

and while this specific use case did not lose much weight the option to get rid of callback arguments stands and might prove useful. Laravel seems to use it a lot.

Time to build something

To keep my feet grounded in utility I will build a small site using the package, my grunt-init templates and the power of Composer in general.