grunt-php-router-gen
Create a Fastest Php router by grunt
Generate JSON routing behavior file
Php Router read the file and find match
that's all.
routerphp--class in attached
Getting Started
This plugin requires Grunt ~0.4.4
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-php-router-gen --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt;
The "php_router_gen" task
Overview
In your project's Gruntfile, add a section named php_router_gen
to the data object passed into grunt.initConfig()
.
grunt;
Options
options.read (fpath)
Type: function
Method to open file.
// example (default) { var readMethod = 'read'; if /.json$/ readMethod = 'readJSON'; else if /.yml$/ readMethod = 'readYML'; return gruntfilereadMethodfpath;}
options.parse (fileContent)
Type: function
Method for parsing file
// example (default) { return fileContent; // must return array of rows of routes}
// MUST RETURN ARRAY of OBJECTS name: 'someName' // required method: 'get' url: 'url/to/parse/(:args)/(...)' // required
options.collect
Type: function
Method for compile route to json
{ return _;}
options.collectPattern (urlPattern, route)
Type: function
Method for compile url-pattern from urlSegments and route options
{ var url = _; var lastSlash = routeOptionstrailingSensitive ? routeOptionshasLastSlash ? '/' : '' : '[/]?'; var flags = routeOptionscaseSensitive ? '' : 'i'; flags += routeOptionsunicodeSensitive ? 'u' : ''; url = '#^/?' + url + lastSlash + '$#' + flags; return url;}
options.collectReverse
Type: function
Method for compile reverse url-pattern from urlSegments and route options
{ var r = _; r = r; r = utils; return r;}
options.caseSensitive
Type: boolean
Default value: true
option for reg exp of end url-pattern
options.unicodeSensitive
Type: boolean
Default value: true
option for reg exp of end url-pattern
options.trailingSensitive
Type: boolean
Default value: true
Option for reg exp of end url-pattern. Last slash sensitivity. Url dependence of slash if true. add [/]? in end if else
options.beautify
Type: boolean
Default value: false
JSON stringify option (add indent if true)
options.map
Type: object|function|null
Default value: null
route's data (such as params and other). This object will be returned if route found match with route.uri
someRouteName: params: someUrlParamName: defaultValue1 someParamName: defaultValue2
options.getRouteParamsByMap (routeName, map)
Get route data from route-data-map.
// example { if maprouteName == null gruntfail; return maprouteName;}
options.aliases
Type: object
Default value: {}
Constraint aliases. for use in url patterns as (decimal:paramName)
'decimal': '\\d+' 'integer': '-?\\d+'
options.parseUrlSegment
Type: function
Detect args in current url segment.
default detection format of simple url param: (PATTERN_OR_ALIAS:PARAM_NAME) default detection format of infinite url params: (...)
//MUST RETURNsegmentObj = args: name: 'strName' // string name mast compatible with template /^[a-z][a-zA-Z0-9_]$/ pattern: '(?P<strName>.+)' // VALID NAMING REG_EXP PATTERN FOR DETECT PARAM FROM THIS SEGMENT STRING name: 'strName' pattern: '(?P<strName>.+)' segment: strSegmentPattern // string;return segmentObj;
Usage Examples
source/routes.json
[
{
"name": "hello-1",
"url": "test/one/((\\d+|_\\d+):var1)/",
"method": "get"
},
{
"name": "one-hello",
"url": "test/one/(:var1)/",
"method": "get"
},
{
"name": "two-hello",
"url": "test/one/(:var1)",
"method": "get"
},
{
"name": "tree-hello",
"url": "test/one/(:var1)/222",
"method": "get"
}
]
gruntfile
In this example, custom options are used to do something else with whatever else. So if the testing
file has the content Testing
and the 123
file had the content 1 2 3
, the generated result in this case would be Testing: 1 2 3 !!!
grunt;
ADVANCE USAGE
routes test/two/routes_two.txt
this file translate into need format. but this format better for routing
get / -> user.post
get user/(...)? -> user.post
get user -> user.get.one #line comment
post user -> user.post
put user/(...) -> user.post
get user/(...)?/123 -> user.post
get session/(...)?/123 -> session.get
#line comment
get /user/(decimal:id)?/asd -> user.get.all
get user/(\d+:id)? -> user.get.all2
get hello/(\d+:ggg)? -> hello.get
map test/two/map_two.json
"root": "/controllers" "<user>": "path": "/user" "<get>": "<all>": {} "<all2>": {} "<one>": {} "<post>": {} "<hello>": "params": "ggg": 111 "<get>": "params": "ggg": 222 "<session>": "params": "aaaa": 111 "<get>": "params": "aaaa": 222
gruntfile
grunt;
usage in PHP
detect current route
require_once('Router.php'); $routes = json_decode(file_get_contents('../../tmp/routes_one.json'), true); $router = new Router($routes);$result = $router->match($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']); var_dump($result);
get url by route name and params (reverse)
require_once('Router.php');
$routes = json_decode(file_get_contents('routes_two.json'), true);
$router = new Router($routes);
$result = $router->reverse('GET', 'user.get.all', array('version'=>333, 'id' => 3));
$result = $router->reverse('post', 'user.post', array('version'=>333, 'id' => 3));
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.