插件目录大致如下图所示
start.php 是一个插件的入口跟识别文件
建立相关文件之后 进入admin管理后台 -plugin 设置中激活 helloworld插件。
pages目录跟views目录分别充当着控制器跟视图的角色
代码如下:
<?php
/**
** hello world 插件
*start.php
*/
elgg_register_event_handler('init', 'system', 'hello_word_init');
function hello_word_init() {
elgg_register_page_handler('hello', 'hello_page_handler');
$item = new ElggMenuItem('hello', 'Hello', 'hello/world');
elgg_register_menu_item('site', $item);
elgg_register_menu_item('page', array(
'name' => 'world',
'text' => 'Hello world',
'href' => 'hello/world',
'contexts' => array('hello'),
));
elgg_register_menu_item('page', array(
'name' => 'dolly',
'text' => 'Hello dolly',
'href' => 'hello/dolly',
'contexts' => array('hello'),
));
}
function hello_page_handler($page, $identifier) {
$plugin_path = elgg_get_plugins_path();
$base_path = $plugin_path . 'hello_world/pages/hello_world';
switch ($page[0]) {
case 'world':
require "$base_path/world.php";
break;
case 'dolly':
require "$base_path/dolly.php";
break;
default:
echo "request for $identifier $page[0]";
break;
}
// return true to let Elgg know that a page was sent to browser
return true;
}
//end file of start.php
<?php
/**
* dolly.php
*/
$user = elgg_get_logged_in_user_entity();
$title = "My second page";
$content = 'hello dolly';
$vars = array(
'content' => $content,
);
$body = elgg_view_layout('one_sidebar', $vars);
echo elgg_view_page($title, $body);
//end file of dolly.php
<?php
/**
* world.php
*/
$time=date('Y-m-d H:i:s',time());
$title = "My first page";
//$content = "Hello, World! <br>".$time;
$content=elgg_echo('hello:word');
$vars = array(
'content' => $content,
);
$body = elgg_view_layout('one_sidebar', $vars);
echo elgg_view_page($title, $body);
//end file of wolrd.php
<?php
/**
* 语言包
*/
$english = array(
'hello:word'=>'您好!!',
);
add_translation("en", $english);
?>