P
P
Pavel2020-11-06 15:40:52
Laravel
Pavel, 2020-11-06 15:40:52

Identical configs in widgets, how to optimize?

Hello!

I have many, many widgets, they all use the same configs, or almost the same, can they be taken out somehow?

The documentation https://github.com/arrilot/laravel-widgets seems to have information, but not enough understanding of how to do it

from documentation

Note 2: You may want (but you probably don't) to create your own BaseWidget and inherit from it. That's fine. The only edge case is merging config defaults from a parent and a child. In this case do the following:

Do not add protected $config = [...] line to a child.

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

    parent::__construct($config);
}



my duplicate code:
tk

class Ac extends AbstractWidget
{
    /**
     * The configuration array.
     *
     * @var array
     */
    
    protected $config = [
        'block_id' => null,
        'site_id' => null,
        'type' => null,
        'view' => null,
        'block_title' => null,
        'block_offer' => null,
        'block_description' => null,
        'image' => null,
        'form' => null,
        'form_id' => null,
        'form_btn_name' => null,
        'folder' => null,
        'pack' => null,
        'item' => null,
        'size' => null,
        'count' => null,
        'back' => null,
        'main' => null,

        'section_id' => null,
        'category_id' => null,
        'item_id' => null,
    ];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        $site_id = $this->config['site_id'];
        $view = $this->config['view'];
        $type = $this->config['type'];
        $manager_id = $this->config['manager_id'];
        $item = $this->config['item'];

        $count = $this->config['count'];

        $back = $this->config['back'];
        $block_id = $this->config['block_id'];
        $block_title = $this->config['block_title'];
....


Gentlemen, from experience, can you show by example how to implement this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question