IntroductionGene \ Router is one of the core classes of the gene framework. The biggest difference between this framework and other common frameworks is its unique, powerful, and simple routing definition.
Powerful and flexible routing, support for callback and class methods; Support for rest and http request methods (get, post, put, patch, delete, trace, connect, options, head; global and local hooks can be defined. You can define global hooks for each method.
How to Set route SeriesMethods: get, post, put, patch, delete, trace, connect, options, head;
Set the routing method, covering the basic methods of rest operations. The routing path supports parameters. The callback method supports class methods and anonymous functions. The hook defines the routing hook and global hook.
Parameters:
Parameters |
Type |
Required |
Note |
RouterPath |
String |
Yes |
Route path, supporting parameters. |
Callback |
String or Closure |
Yes |
The callback method supports class and anonymous functions. |
Hook |
String |
No |
Route hook, set hook, and set global hook. |
Return Value: Gene \ Router instantiate the object itself.
Example 1: routing settingsWhen you access the route address, the method of the Set class is called. Class Method format: classNanme @ methodName.
For example, index @ list;
// Instantiate the route object $ router = new Gene \ Router (); // route setting class method $ router-> get ("/", "Controllers_Index @ index ");
Example 2: Set an anonymous function for a routeWhen you access the route address, the configured anonymous function is called.
// Instantiate the route object $ router = new Gene \ Router (); // set the anonymous function $ router-> get ("/", function () {echo "index ";});
Instance 3: supported Routing ParametersWhen you access the route address, the defined route parameters are passed to the callback method.
Parameter format: add the parameter name after the colon.
For example, ": id ".
Get parameters in the callback method: the parameters in the route (which may have multiple) are passed as an array to the callback method, which can be obtained directly.
// Instantiate the route object $ router = new Gene \ Router (); // route settings support parameters $ router-> get ("/news/: id.html", function ($ params) {echo "id:", $ params ['id'];});
Instance 4: Route settings hookYou can set a hook for the current route, or set a global hook, or disable a global hook.
Hook format: curHookName @ globalHook.
GlobalHook parameter list: clearBefore disables the prefix global hook.
ClearBefore disable the rear global hook
ClearAll disables the front and back global hooks.
Note: After hooks are set in a route, you need to call the hook definition method to define the required hooks.
// Instantiate the route object $ router = new Gene \ Router (); // route setting hook $ router-> get ("/admin/index", Controllers_Admin_Index @ index, "adminHook");/* define the background administrator permission check hook */$ router-> hook ("adminHook", function () {if (! Isset ($ _ SESSION ['admin'] ['user _ id']) {die ('no operation permission! ');}});
HookDefine hooks. You can customize hook names (except for before and after ).
Before: the default front-end global hook;
After: the default global hook is used;
After the global hook is defined, it will be executed by default, unless the rule is declared in the route definition:
For example, exclude the pre-Global HOOK: @ clearBefore;
Parameters:
Parameters |
Type |
Required |
Note |
HookName |
String |
Yes |
Hook name. |
Callback |
String or Closure |
Yes |
The callback method supports class and anonymous functions. |
Return Value: Gene \ Router instantiate the object itself.
Instance 1: Define the front global hook// Instantiate the route object $ router = new Gene \ Router (); // define the prefix global hook $ router-> hook ("before", function () {echo "before ";});
Example 2: define a global hook// Instantiate the route object $ router = new Gene \ Router (); // define the rear global hook $ router-> hook ("after", function ($ params) {echo "after"; if (is_array ($ params) var_dump ($ params );});
Instance 3: Custom hooks// Instantiate the route object $ router = new Gene \ Router (); // define a custom hook $ router-> hook ("webCheck", function () {isset ($ _ SESSION) | session_start (); if (! Isset ($ _ SESSION ['user'] ['user _ id']) {header ('/login.html', 302); die ;}});