title |
---|
Laravel 5 Widget |
Open your composer.json file and add the new required package.
"pingpong/widget" : "~2.1"
Next, open your terminal and run composer update
.
After composer updated, add new service provider in config/app.php
:
'Pingpong\Widget\WidgetServiceProvider',
And add facade in the same file
'Widget' => 'Pingpong\Widget\WidgetFacade'
Done.
Subscribe widget: It's a new way to register widget using a specified class. For example:
Widget::subscribe('WidgetSubscriber');
class WidgetSubscriber {
public function subscribe($widget)
{
$widget->register('image', __CLASS__ .'@image');
}
public function image()
{
return 'Your handler here';
}
}
You can also specified which method to handle subscriber of widget.
Widget::subscribe('WidgetSubscriber@handle');
class WidgetSubscriber {
public function handle($widget)
{
$widget->register('image', __CLASS__ .'@image');
}
public function image()
{
return 'Your handler here';
}
}
By default you can register a widget in app/widgets.php
, that file will autoload automatically.
Via Closure.
// app/widgets.php
Widget::register('small', function($contents)
{
return "<small>{$contents}</small>";
});
Widget::register('view', function($view, $data = array(), $mergeData = array()
{
return View::make($view, $data, $mergeData)->render();
});
Via Class Name.
By default will call register
method.
class MyWidget {
public function register($contents, $attributes = array())
{
$attributes = HTML::attributes($attributes);
return "<h1{$attributes}>{$contents}</h1>";
}
}
Widget::register('h1', 'MyWidget');
Via Class Name with the specified method.
class TagCreator {
public function create($tag, $contents, $attributes = array())
{
$attributes = HTML::attributes($attributes);
return "<{$tag}{$attributes}>{$contents}</{$tag}>";
}
}
class HTMLWidget {
protected $tag;
public function __construct(TagCreator $tag)
{
$this->tag = $tag;
}
public function p($contents, $attributes = array())
{
return $this->tag->create('p', $contents, $attributes);
}
public function div($contents, $attributes = array())
{
return $this->tag->create('div', $contents, $attributes);
}
}
Widget::register('p', 'HTMLWidget@p');
Widget::register('div', 'HTMLWidget@div');
Widget::get('small', array('My Content'));
Widget::call('small', array('My Content'));
Widget::small('My Content');
Widget::p('My Content');
Widget::div('My Content');
Widget::h1('My Content');
On view you can call like this.
@small('My Content')
@view('users.show', $data, $mergeData)
@h1('Welcome!')
@p('Hello World', array('class' => 'page-header'));
@div('Lorem ipsum', array('class' => 'alert alert-warning'));
It is very easy to group widget. you only need to specify the group name and specify an array of the names of the widgets that will be grouped.
Widget::register('calendar', 'SidebarWidget@calendar')
Widget::register('archive', 'SidebarWidget@archive')
Widget::group('sidebar', array('calendar', 'archive'));
To call a group of widgets is the same as calling the widget.
Widget::sidebar();
If you want to send parameters to the widget that is in the group, you can call it like this.
Widget::sidebar(
array('your-first-param', 'your-second-param'),
array('first-param-for-second-widget', 'the-second')
);
On view you can call a group of widgets is same as calling the widget.
@sidebar()
@sidebar(array('first-param'), array('first-param-for-second-widget'))