Gene Framework Document, gene framework

Source: Internet
Author: User

Gene Framework Document, gene framework
Routing class Gene \ Router

  • Introduction

    Gene \ 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.

  • Instantiation

    Initialize the route and pass the cache key. The route configuration will be cached.
    If Gene \ Router is used as the routing configuration of gene_application, the cache key can be passed without the application flag name. If Gene \ Router is used as an independent function, the cache key must be passed, different routing configurations.

    Parameters:
    Parameters Type Required Note
    KeyName String No Route cache flag

    Return Value: Gene \ Router instantiate the object itself.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router ();
  • How to Set route Series

    Methods: 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 settings

    When 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 route

    When 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 Parameters

    When 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 hook

    You 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! ');}});
  • Hook

    Define 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 ;}});
  • Group

    Routing group method.
    If multiple routing definitions share a common prefix, you can use the group method to define the prefix.
    The group method must be used in pairs to form a closed loop. Other similar functions: prefix.

    Parameters:
    Parameters Type Required Note
    Name String No Group name. If the parameter is not null, the group is opened. If the parameter is null, the group is closed;

    Return Value: Gene \ Router instantiate the object itself.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); // route group: put the background user management in a group. $ router-> group ("/admin/user")-> get ("/add", "Controllers_Admin_User @ add ", "adminAuth @ clearAll")-> post ("/addPost", "Controllers_Admin_User @ addPost", "adminAuth @ clearBefore")-> get ("/edit/: id ", "Controllers_Admin_User @ edit", "adminAuth @ clearAll")-> post ("/editPost", "Controllers_Admin_User @ editPost", "adminAuth @ clearBefore ") -> get ("/del/: id", "Controllers_Admin_User @ del", "adminAuth @ clearBefore")-> group ();
  • Error

    Define route error handling.
    The 401 processing hook is built in by default. If the 401 error is defined, the route processing will be automatically called if it fails.

    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:
    // Instantiate the route object $ router = new Gene \ Router (); // defines 401 $ router-> error (401, function () {echo "401 ";});
  • GetTime

    Obtain the Cache Time of the current route configuration.

    Parameters:

    None

    Return Value: String.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ time = $ router-> getTime ();
  • GetEvent

    Obtains the cached events of the current route.

    Parameters:

    None

    Return Value: Array.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ event = $ router-> getEvent ();
  • GetTree

    Obtain the routing definition of the current route cache.

    Parameters:

    None

    Return Value: Array.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ tree = $ router-> getTree ();
  • DelEvent

    The time when the current route cache is deleted.

    Parameters:
    Parameters Type Required Note
    KeyName String No Cache name. The current project is deleted by default.

    Return Value: Boolean.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ result = $ router-> delEvent ();
  • DelTree

    Delete the routing definition of the current route cache.

    Parameters:
    Parameters Type Required Note
    KeyName String No Cache name. The current project is deleted by default.

    Return Value: Boolean.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ result = $ router-> delTree ();
  • Clear

    Clears the routing definitions and events of the current route cache.

    Parameters:
    Parameters Type Required Note
    KeyName String No Cache name. The current project is deleted by default.

    Return Value: Boolean.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ result = $ router-> clear ();
  • GetRouter

    Obtain the route object.

    Parameters:

    None

    Return Value: Gene \ Router instantiate the object itself.

    Instance:
    // Obtain the instantiated route object $ router = Gene \ Router: getRouter ();
  • Run

    After this method is executed, the route definition is run. By default, web applications do not pass parameters. If you access or execute tasks in cli mode, you need to pass parameters.
    The run method also exists in gene_application, which is actually called in Gene \ Router.
    The difference between the two is that gene_application encapsulates the configuration loading and updating logic, and will not be loaded repeatedly without modification, achieving high efficiency.
    No cache judgment is processed in Gene \ Router. If the Gene \ Router module is used separately, you need to use the getTime method to obtain the cache time for judgment and processing.

    Parameters:
    Parameters Type Required Note
    Method String No Request Method (default automatic access method: get, post, etc)
    Path String No Request Path (the access path is automatically used by default, for example,/admin/login)

    Return Value: Gene \ Router instantiate the object itself.

    Example 1: web Applications
    // Instantiate the route object $ router = new Gene \ Router (); // configure the route $ router-> clear ()-> get ("/", function () {echo "index" ;})-> run ();
    Example 2: cli Application
    // Instantiate the route object $ router = new Gene \ Router (); // configure the route $ router-> clear ()-> get ("/", function () {echo "index" ;})-> run ('get', '/admin/task ')
  • RunError

    Run the defined error.

    Parameters:
    Parameters Type Required Note
    ErrorName String Yes Error name.

    Return Value: Boolean.

    Instance:
    // Instantiate the route object $ router = new Gene \ Router (); $ result = $ router-> runError ("401 ");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.