Mecha CMS

Deep learning on using the API.

Class Route

Author: Taufik Nurrohman · 65 Views

Tweet

Do something if URL path matched with pattern.

Table of Content

Patterns 

PatternDescription
%s%Matched with any but /
%i%Matched with any numbers.
%f%Matched with any numbers and floats.
%*%Matched with any and /
%[foo,bar,baz\,qux]%Matched with foo, bar and baz,qux
%[foo,bar,%i%]%Matched with foo, bar and any numbers.

Set 

Route::set($id = null, $fn = null, $stack = null, $pattern = false);

Set a new route:

Route::set('blog/%i%', function($step = 1) {
    // here, `$step` will return `4` if we are in `‌http://mecha-cms.com‌/blog/4`
    // and will return `1` if we are in `‌http://mecha-cms.com‌/blog`
});

Note: Default $stack value is 10.

Set multiple routes:

Route::set(['blog/%s%/%i%', 'blog/%s%', 'blog/%i%', 'blog'], function($slug = "", $step = 1) { … });

The code above is equal to these:

Route::set('blog/%s%/%i%', function($slug = "", $step = 1) { … }, 10);
Route::set('blog/%s%', function($slug = "", $step = 1) { … }, 10.1);
Route::set('blog/%i%', function($slug = "", $step = 1) { … }, 10.2);
Route::set('blog', function($slug = "", $step = 1) { … }, 10.3);

Reset 

Route::reset($id);

Remove a route:

Route::reset('blog/%s%');

Remove routes:

Route::reset(['blog/%s%/%i%', 'blog/%s%']);

Get 

Route::get($id = null, $fail = false);

Check whether blog/%s% route is exists:

if (Route::get('blog/%s%')) { … }

This method will return the route data that has been set by Route::set(); or false:

__test__(
    Route::get('blog/%s%'),
    Route::get('blog/$'),
    Route::get('blog/$', 0)
);

Result:

[
    'fn' => function($slug = "", $step = 1) { … },
    'stack' => 10,
    'is' => ['pattern' => false]
]
false
0

Pattern 

Route::pattern($pattern, $fn = false, $stack = null);

Set a new route or routes using plain regular expression pattern:

Route::pattern('#^blog(?:\/([^\/]+))?(?:\/(\d+))?$#', function($slug = "", $step = 1) { … });

Is 

Route::is($id, $fail = false, $pattern = false);

Detect whether current URL path is matched with the route pattern:

if (Route::is('blog/%s%')) { … }

This method will return the route data, parsed from the current URL path; or false:

__test__(
    Route::is('blog/%s%/%i%'),
    Route::is('blog/$'),
    Route::is('blog/$', 0)
);

Result:

[
    'pattern' => 'blog/%s%/%i%',
    'path' => 'blog/archive/4',
    'lot' => ['archive', 4]
]
false
0

Fire 

Route::fire($id = null, $lot = []);

Execute the added routes:

Route::fire();

Execute specific route with pre–defined value:

Route::fire('blog/%s%/%i%', ['archive', 4]);

Lot 

Route::lot($id, $fn = null, $stack = null, $pattern = false);

Set custom actions before blog/%s% route execution:

function my_route_hook_fn($slug = "") {
    $_POST['foo'] = 'bar';
}

Route::lot('blog/%s%', 'my_hook_fn');

Note: Default $stack value is 10.

Contain 

Route::contain($id = null, $stack = null | $fn = null, $fail = false);

Check whether my_route_hook_fn already hooked to the blog/%s% route:

if (Route::contain('blog/%s%', 'my_route_hook_fn')) { … }

0 Comments

No comments yet.