使用基于 php 的平台的钩子向上移动 DOM


Moving up the DOM with hooks for a php based platform

我正在尝试为一个名为Ushahidi的平台构建我的第一个插件。Ushahidi是一个使用Kohana框架的基于PHP的平台。

我在这里查看所有可用的钩子:https://wiki.ushahidi.com/display/WIKI/Plugin+Actions

我的目标是在某些页面的标题中添加元标记,以帮助使网站更易于搜索和共享。这些标签将根据页面内容动态显示,但现在我只想将"Hello World"放在正确的位置。

能找到的最接近的钩子将我带到正确的页面,但没有正确的位置。如果您访问 http://advance.trashswag.com/reports/view/1 我已经设法让字符串"Hello World"出现在页面上。第 1 步完成 - 太好了。对我来说,第 2 步是让 hello world 出现在页面标题中,以便只能使用"查看页面源代码"查看。有没有办法根据我的函数备份 DOM:

<?php
class SearchShare{
    public function __construct(){
        //hook into routing
        Event::add('system.pre_controller', array($this, 'SearchShare'));
    }
    public function SearchShare(){
        // This seems to be the part that tells the platform where to place the change. Presumably this is the part I'd need to edit to step up the DOM into the head section
        Event::add('ushahidi_action.report_meta', array($this, 'AddMetaTags'));
    }
    public function AddMetaTags(){
        // just seeing if I can get any code to run
        echo '<h1 style="font-size:70px;">Hello World</h1>';
    }
}
new SearchShare;
?>

您需要使用不同的事件才能将代码放在正确的位置。有几个地方可以挂钩:

  1. 使用 ushahidi_action.header_scripts 事件:

    Event::add('ushahidi_action.header_scripts', array($this, 'AddMetaTags'));

    请参阅标题.php以查看其挂钩的位置。

  2. 使用 ushahidi_filter.header_block 事件:

    public function SearchShare(){
      Event::add('ushahidi_filter.header_block', array($this, 'AddMetaTags'));
    }
    public function AddMetaTags(){
      $header = Event::$data;
      $header .= "Hello World";
      Event::$data = $header;
    }
    
    请参阅主题.php了解其挂钩的位置。

这些都不比另一个更好/更差,所以使用你喜欢的任何一个。