zoukankan      html  css  js  c++  java
  • HelpersHooks

    HelpersHooks

    Add modules with hooks

    The hooks helper allows modules to be created within the module folder. Hooks allow code to be injected into various parts of the framework as well as creating new routes for modules to use.

    The following hooks have been created and placed in the following files to allow you to make use of them.

    • meta - placed in app/templates/default/header.php
    • css - placed in app/templates/default/header.php
    • afterBody - placed in app/templates/default/header.php
    • footer - placed in app/templates/default/footer.php
    • js - placed in app/templates/default/footer.php
    • routes - placed in index.php

    The hooks: meta, css, afterBody, footer and js are placeholders to execute code from modules. The routes hook enables custom routes to be defined from a module.

    Place a hook where it can be executed

    To use a hook first make reference to it:

    use HelpersHooks;
    
    $hooks = Hooks::get();

    Once initiated calls can be called by running $hooks->run('hookName'), this will then run all code attached to the hook for instance to run all code attached to the css hook (used for injecting css files):

    <?php
    
    use HelpersHooks;
    
    //initialise hooks
    $hooks = Hooks::get();
    ?>
    <!DOCTYPE html>
    <html lang="<?php echo LANGUAGE_CODE; ?>">
    <head>
        <?php
        //hook for plugging in css
        $hooks->run('css');
        ?>

    Add code to a hook

    To add code to a hook call Hooks::addHook() this method expects 2 parameters:

    • The hook to inject into.
    • The path to the class, start with the namespace followed by the class then @method to be called.
    use HelpersHooks;
    
    Hooks::addHook('css', 'AppModelsDemoControllersDemo@css');

    Once run any code inside the class method will be executed on the page where the hook is being called.

    Calling custom routes

    Define the routes hook and pass in a path to the class and method to be used.

    use HelpersHooks;
    
    Hooks::addHook("routes", 'AppModulesDemoControllersDemo@routes');

    Next in the class create an alias for Router. Next in the method the routes will be used in call Router followed by the route desired, instead of calling a controller use a closure to run the code against the route.

    <?php
    namespace AppModulesDemoControllers;
    
    use CoreRouter;
    
    class Demo
    {
        public function routes()
        {
            Router::any('democall', function () {
                echo 'called from demo module';
            });
        }
    }

    In the above example a route is defined, it's called when the example.com/democall is ran in the address bar. This closure echo's a message. The code inside the closure can call views and models the same as controller methods do.

    Creating a Module

    To create a module create a folder inside the Modules folder, the convention is to use StudyCase for example a module called demo should name named Demo. Inside the module folder create a Controllers, Models and views folder, those folders should hold the classes for controllers and models. The views is for the output of the module pages.

    index.php should also be created in the route of the module folder, this file defined all hooks the module will be attached to.

  • 相关阅读:
    git clone 慢,使用镜像
    Mysql 插入 path 插入不进去值
    os.walk 获取文件夹下所有的文件
    Manjaro安装后你需要这样做(仅有网址)
    Mysql 查询优化
    pandas df.to_csv 可保存为 txt 类型 index 设置索引 header 列名 sep 使用什么进行分隔
    pandas pd.to_markdown() 转换为 Markdown pd.to_latex() 转换为 latex
    pandas 读取txt seq分隔符类型 engine指定引擎 header 不将第一行作为列名
    pandas 读取文件时 header设置列名 index_col 设置索引 usecols 读取哪几列 parse_dates 将哪一列设置为时间类型 nrows 读取数据行数
    numpy cumprod 累乘 cumsum 累加 diff 和前一个元素做差值
  • 原文地址:https://www.cnblogs.com/chunguang/p/5643184.html
Copyright © 2011-2022 走看看