CodeIgniter 3 Widget for reusable building view blocks
This Widget extension is collected into yidas/codeigniter-pack which is a complete solution for Codeigniter framework.
Common interface with Yii2 pattern like
Reusable building blocks implementation
PSR-4 Namespace support for static call
Define a widget then use it in Codeigniter's view:
<?php
use app\widgets\Hello;
?><div><?=Hello::widget(['message' => 'Good morning']);?></div>This library requires the following:
- PHP 5.4.0+
- CodeIgniter 3.0.0+
- yidas/codeigniter-psr4-autoload 1.0.0+
Run Composer in your Codeigniter project under the folder \application:
composer require yidas/codeigniter-widget
Check Codeigniter application/config/config.php:
$config['composer_autoload'] = TRUE;You could customize the vendor path into
$config['composer_autoload']
Widget extension require yidas/codeigniter-psr4-autoload to implement PSR-4 Namespace, which you need to configure at first:
The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php file:
$config['enable_hooks'] = TRUE;Hooks are defined in the application/config/hooks.php file, add above hook into it:
$hook['pre_system'][] = [newyidas\Psr4Autoload, 'register'];After the configuration, you are able to create widgets.
To create a widget, extend from yidas\Widget and override the init() and/or run() methods, remember to defind this widget a current namespace refering by yidas/codeigniter-psr4-autoload.
init() contains the code that initializes the widget properties.
run() contains the code that generates the rendering result of the widget.
In the following example, Hello widget display the partial view with assigning to its message property. If the property is not set, it will display your Codeigniter base_url by default. This widget file should place in application/widgets/Hello.php:
<?phpnamespaceapp\widgets;
useyidas\Widget;
class Test extends Widget
{
// Customized variable for Widgetpublic$message;
publicfunctioninit()
{
// Use $this->CI for accessing Codeigniter object$baseUrl = $this->CI->config->item('base_url');
if ($this->message === null) {
$this->message = "Your Site: {$baseUrl}";
}
}
publicfunctionrun()
{
// Render the view `test.php` in `WidgetPath/views` directory,return$this->render('test', [
'message' => $this->message,
]);
}
}yidas/codeigniter-psr4-autoload provides the PSR-4 Namespace ability for Codeigniter framework.
public string render(string $view, array $variables=[])By default, views for a widget should be stored in files in the WidgetPath/views directory, where WidgetPath stands for the directory containing the widget class file.
Therefore, the above example will render the view file app/widgets/views/hello.php, assuming the widget class is located under app/widgets.
You may override the yidas\Widget::getViewPath() method to customize the directory containing the widget view files.
Example:
publicfunctionrun()
{
return$this->render('view_name', [
'variable' => $this->property,
]);
}Widget already prepared $CI property which is CodeIgniter Resources object, you could access it by $this->CI in your widget's init() or run() methods.
publicfunctionrun()
{
// Get data from a model$this->CI->load->model('Menu_model');
$list = $this->CI->Menu_model->getList();
// Build widget's view with list datareturn$this->render('test', [
'list' => $list,
]);
}Statically call widget() with config in view, and the widget would render into that place:
<?php
use app\widgets\Hello;
?><div><?=Hello::widget(['message' => 'Good morning']);?></div>