Home >Backend Development >PHP Tutorial >How to integrate widget (page layout) with CI framework, ciwidget_PHP tutorial
This article describes how the CI framework integrates widgets (page layout). Share it with everyone for your reference, the details are as follows:
In the process of WEB development, we inevitably have to output view files, and usually view files generally contain common elements such as the header, left column, and bottom.
How to introduce agile thinking into the entire project development when it comes to view application and improve efficiency? I have to mention widgets. Of course you can also use the HMVC model.
Here we only introduce how widgets are integrated into the CI framework:
1. Add the calling method in the class file MY_Controller.php:
/* * 如果$name存在则调用widget类及widget方法 *@param string $name */ protected function widget ($name = '') { if (isset($name) && $name != '') { require_once BASE_WIDGET.$name.'.php'; } }
2. Define widget class file:
<?php /*------------ widget.php 组件包含自己的控制器,视图,模型(可以共用普通model) *用来实现各页面都有的公共部分 @author crystal 20120106 --------------*/ class Widget extends MY_Controller { private function __construct() { parent::MY_Controller(); } /*** 获取当前类名*/ private static function _getClass() { return __CLASS__; } public static function left() { $class = self::_getClass(); $data['userinfo'] = MY_Controller::_getUserCookieInfo(); $this->load->view('com/left.php',$data); } } ?>
3. Call in the view file (the widget method in the parent class control should be called first in the corresponding controller):
<!--left sider --> <div> <?php Widget::left();?> </div> ... <!--/left sider -->
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.