yidas / codeigniter-widget
Developing
Installs: 2 884
Dependents: 1
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 8
Open Issues: 1
Requires
- php: >=5.4.0
- yidas/codeigniter-psr4-autoload: ~1.0.0
This package is auto-updated.
Last update: 2024-11-29 05:09:17 UTC
README
CodeIgniter Widget
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.
Features
-
Common interface with Yii2 pattern like
-
Reusable building blocks implementation
-
PSR-4 Namespace support for static call
OUTLINE
DEMONSTRATION
Define a widget then use it in Codeigniter's view:
<?php use app\widgets\Hello; ?> <div> <?=Hello::widget(['message' => 'Good morning']);?> </div>
REQUIREMENTS
This library requires the following:
- PHP 5.4.0+
- CodeIgniter 3.0.0+
- yidas/codeigniter-psr4-autoload 1.0.0+
INSTALLATION
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']
CONFIGURATION
Widget extension require yidas/codeigniter-psr4-autoload to implement PSR-4 Namespace, which you need to configure at first:
1. Enabling Hooks
The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php
file:
$config['enable_hooks'] = TRUE;
2. Adding a Hook
Hooks are defined in the application/config/hooks.php
file, add above hook into it:
$hook['pre_system'][] = [new yidas\Psr4Autoload, 'register'];
After the configuration, you are able to create widgets.
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
:
<?php namespace app\widgets; use yidas\Widget; class Test extends Widget { // Customized variable for Widget public $message; public function init() { // Use $this->CI for accessing Codeigniter object $baseUrl = $this->CI->config->item('base_url'); if ($this->message === null) { $this->message = "Your Site: {$baseUrl}"; } } public function run() { // 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.
Rendering View
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:
public function run() { return $this->render('view_name', [ 'variable' => $this->property, ]); }
Utilizing CodeIgniter Resources
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.
public function run() { // Get data from a model $this->CI->load->model('Menu_model'); $list = $this->CI->Menu_model->getList(); // Build widget's view with list data return $this->render('test', [ 'list' => $list, ]); }
USAGE
Widget on View
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>