gpt4 book ai didi

php - 如何在 Codeigniter 中创建小部件系统

转载 作者:IT王子 更新时间:2023-10-29 00:12:42 25 4
gpt4 key购买 nike

我正在 Codeigniter 中创建一个自定义 CMS,我希望有一个类似于 Wordpress 中使用的小部件系统。

例如,我想要一个小部件来显示侧边栏上显示的最后 5 个帖子。我还希望能够逐页控制此小部件显示的页面。

我正在使用 Phil Sturgeon's Template library ,所以一个示例 Controller 看起来像:

$this->template->set_partial('header', 'layouts/header');
$this->template->set_partial('footer', 'layouts/footer');
$this->template->set_partial('sidebar', 'layouts/sidebar');

$this->data['title'] = "Create Post";
$this->template->build('create', $this->data);

我想坚持使用 MVC 模式,所以我不想将逻辑放在侧边栏 View 中,这是我现在唯一能想到的。

我应该为此使用 HMVC 吗?

我如何告诉侧边栏要显示哪些小部件?

最佳答案

这是来自 Wiredesignz 的 Widget 库

Read more information

/**
* Widget Plugin
*
* Install this file as application/plugins/widget_pi.php
*
* @version: 0.21
* $copyright Copyright (c) Wiredesignz 2009-09-07
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Widget
{
public $module_path;

function run($file) {
$args = func_get_args();

$module = '';

/* is module in filename? */
if (($pos = strrpos($file, '/')) !== FALSE) {
$module = substr($file, 0, $pos);
$file = substr($file, $pos + 1);
}

list($path, $file) = Modules::find($file, $module, 'widgets/');

if ($path === FALSE) {
$path = APPPATH.'widgets/';
}

Modules::load_file($file, $path);

$file = ucfirst($file);
$widget = new $file();

$widget->module_path = $path;

return call_user_func_array(array($widget, 'run'), array_slice($args, 1));
}

function render($view, $data = array()) {
extract($data);
include $this->module_path.'views/'.$view.EXT;
}

function load($object) {
$this->$object = load_class(ucfirst($object));
}

function __get($var) {
global $CI;
return $CI->$var;
}
}

示例

// application/widgets/Hello_world.php
class Hello_world extends Widget
{
function run() {
$this->render('hello_world');
}
}

在您的 View 中调用小部件类的静态“run”方法:

widget::run('hello_world');

关于php - 如何在 Codeigniter 中创建小部件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11104630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com