PHP code example of artisancms / widgets

1. Go to this page and download the library: Download artisancms/widgets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

artisancms / widgets example snippets




'providers' => [
    ...
    ArtisanCMS\Widgets\ServiceProvider::class,
],



'aliases' => [
    ...
    'Widget'       => ArtisanCMS\Widgets\Facade::class,
    'AsyncWidget'  => ArtisanCMS\Widgets\AsyncFacade::class,
],



namespace App\Widgets;

use ArtisanCMS\Widgets\AbstractWidget;

class RecentNews extends AbstractWidget
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        //

        return view("widgets.recent_news", [
            'config' => $this->config,
        ]);
    }
}

@widget('recentNews')

{{ Widget::run('recentNews') }}

{{ Widget::recentNews() }}

class RecentNews extends AbstractWidget {
    ...
    protected $config = [
        'count' => 5
    ];
    ...
}

...
@widget('recentNews') // shows 5
@widget('recentNews', ['count' => 10]) // shows 10

class RecentNews extends AbstractWidget {
    ...
    protected $config = [
        'count' => 5,
        'foo'   => 'bar'
    ];
    
    ...
}

@widget('recentNews', ['count' => 10]) // $this->config['foo'] is still 'bar'

    public function __construct(array $config = [])
    {
        $this->addConfigDefaults([
            'child_key' => 'bar'
        ]);

        parent::__construct($config);
    }

@widget('recentNews', ['count' => 10], 'date', 'asc')
...
public function run($sortBy, $sortOrder) { }
...
 artisan vendor:publish --provider="ArtisanCMS\Widgets\ServiceProvider"

@widget('News\RecentNews', $config)

@widget('news.recentNews', $config)

@widget('\App\Http\Some\Namespace\Widget', $config)

public function placeholder()
{
    return "Loading...";
}

class RecentNews extends AbstractWidget
{
    /**
     * The number of seconds before each reload.
     *
     * @var int|float
     */
    public $reloadTimeout = 10;
}

    /**
     * Async and reloadable widgets are wrapped in container.
     * You can customize it by overriding this method.
     *
     * @return array
     */
    public function container()
    {
        return [
            'element'       => 'div',
            'attributes'    => 'style="display:inline" class="artisancms-widget-container"',
        ];
    }

class RecentNews extends AbstractWidget
{
    /**
     * The number of minutes before cache expires.
     * False means no caching at all.
     *
     * @var int|float|bool
     */
    public $cacheTime = 60;
}

// add several widgets to the 'sidebar' group anywhere you want (even in controller)
Widget::group('sidebar')->position(5)->addWidget('widgetName1', $config1);
Widget::group('sidebar')->position(4)->addAsyncWidget('widgetName2', $config2);

// display them in a view in the correct order
@widgetGroup('sidebar')
//or 
{{ Widget::group('sidebar')->display() }}
bash
php artisan make:widget RecentNews