我创造了一个笨定制CMS和我想有类似于在WordPress的使用一个小部件系统。
例如,我想有一个小窗口,显示在侧边栏显示的最后5个帖子。 我还希望能够控制这个小部件出现在页逐页哪些页面。
我使用的菲尔鲟鱼的模板库 ,这样的例子控制器的样子:
$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模式,所以我不想把逻辑在侧边栏视图,这是我现在能想到的唯一的事情。
是HMVC不是我应该使用吗?
我怎样才能知道哪些部件显示侧边栏?
下面是从部件库Wiredesignz
了解更多信息
/**
* 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');
}
}
在您看来呼吁Widget类静态的“跑”的方法:
widget::run('hello_world');
如果你想看到一些控制器一些小部件? 在我看来,一些基本的东西,简单就是保持CMS的小部件的名称和一个布尔值 ,有效和无效的数据库。
最后,在查询时,你会得到小部件的数组。 我们可以扩展主控制器和显示的所有窗口小部件默认(使用阵列,以及一个全局变量)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
public $cms_widget = array();
class CMS_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->cms_widget = array(
'msg' => TRUE,
'chat' => TRUE,
'video' => TRUE,
'games' => TRUE
);
}
}
当你打电话给你CMS的模板,你应该做一个有条件的所有部件出现在所需位置。 例如,在列视图模板:
if($this->cms_widget['msg']) $this->load->view('widget/msg_view');
if($this->cms_widget['chat']) $this->load->view('widget/chat_view');
if($this->cms_widget['video']) $this->load->view('widget/video_view');
if($this->cms_widget['games']) $this->load->view('widget/games_view');
现在,例如。 如果你在眼前“游戏”,将只显示相应的控件。 假设我希望看到的窗口小部件“游戏”和“视频”。 我们只需要禁用其余部件
<?php
class Game extends CMS_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// Hidden widgets, replacing the values in the array
$this->cms_widget = array_merge($this->cms_widget, array(
'msg' => FALSE,
'chat' => FALSE
));
$this->load->view('game_view');
}
}
}