Helpers\Hooks

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 Helpers\Hooks;

$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 Helpers\Hooks;

//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 Helpers\Hooks;

Hooks::addHook('css', 'App\Models\Demo\Controllers\Demo@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 Helpers\Hooks;

Hooks::addHook("routes", 'App\Modules\Demo\Controllers\Demo@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 App\Modules\Demo\Controllers; use Core\Router; 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.

Helpers\Hooks的更多相关文章

  1. Config

    Config Config App Auth Cache Database Languages Mail Modules Routing Session Config Settings for the ...

  2. CI框架入门笔记

    当前(2019-03-22)CodeIgniter 框架的最新版本是 3.1.5,于2017年6月发布,距今快两年了也没有更新,这与 Laravel 的更新速度相比差距太大了.因为确实,它是一个很古老 ...

  3. 对CI框架中几个文件libraries

    对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知    时间:2014-10-20 11:37   阅读数:117   作者:xbdadmin [导读] 1.lib ...

  4. ASP.NET Core 中文文档 第四章 MVC(3.6.1 )Tag Helpers 介绍

    原文:Introduction to Tag Helpers 作者:Rick Anderson 翻译:刘浩杨 校对:高嵩(Jack) 什么是 Tag Helpers? Tag Helpers 提供了什 ...

  5. ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)

    原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...

  6. [asp.net core]定义Tag Helpers

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring Getting started wi ...

  7. [asp.net core] Tag Helpers 简介(转)

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...

  8. 使用 Git Hooks 实现自动项目部署

    最近在某服务器上面搭建 git 开发和部署环境,git 开发环境很简单,按照 ProGit 一书的相关知识就可以轻松搞定,实现了类似 Github 的使用 SSH + 私有 Clone 的方式. 关于 ...

  9. CI框架之HOOKS使用流程及原理

        Ci框架中Hooks可以理解:在框架的执行流程过程中,允许开发者在固定的某些时间点上(如:调用控制器前,调用控制器后等时间点上),调用其他函数来扩充CI框架执行流程的一种方法.技术上来就是通过 ...

随机推荐

  1. C++指针的引用

    [1]指针的引用,必须加上头文件<iomanip>因为调用类setw() 对一个数据可以使用“引用”(reference)这是C++           对C的一个重要扩充,引用是一种新的 ...

  2. cocos2d-x 详解之 CCAction(动作)

    关于动作部分,总的来说使用起来比较简单,创建一个动作,然后让可渲染节点如精灵去执行这个动作即可.cocos2dx提供了很多类型的动作,使用起来也很方便.本节重点介绍动作CCAction的子类之一时间动 ...

  3. 记:Tmall活动页面开发

    一.年轻的我 “无人不成商”,如果一个电子商务网站想要做起来,搞活动时必不可少的(引入流量.提高用户黏度.活跃网站氛围),今天打折,明天送红包. (立秋活动,右) 作为一个前端,我当然要从技术的角度来 ...

  4. python学习之random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  5. C++11 之 " = delete "

    1  缺省函数 设计一个类,没有成员函数 (member function),只有成员数据 (member data) class DataOnly { private: std::string st ...

  6. hive-mysql安装配置

    默认情况下,hive的元数据信息存储在内置的Derby数据中.Facebook将hive元数据存储在关系数据库 1.安装好mysql ,sudo apt-get install mysql-serve ...

  7. 基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)

    1.冒泡排序: (1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. (2)外面再套个循环就行. 算法复杂度:O(N2)   不罗嗦,上代码: //冒泡排序(两两交换,外加一个外循环) pub ...

  8. HDU-4742 Pinball Game 3D 三维LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意:求3维的LIS.. 用分治算法搞得,参考了cxlove的题解.. 首先按照x排序,然后每个 ...

  9. 依赖包bcrypt安装Issues

    说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 在决策树项目中,使用到了bcrypt依赖包来加密文件.在wini8(win7)部署安装这个依赖的时候容易出现出现了问题. 解决 ...

  10. Glibc辅助运行库 (C RunTime Library): crt0.o,crt1.o,crti.o crtn.o,crtbegin.o crtend.o

    crt1.o, crti.o, crtbegin.o, crtend.o, crtn.o 等目标文件和daemon.o(由我们自己的C程序文件产生)链接成一个执行文件.前面这5个目标文件的作用分别是启 ...