arrilot / laravel-widgets

Widgets for Laravel
1.12k stars 113 forks source link

Passing variables to widget #140

Closed AlexThousand closed 4 years ago

AlexThousand commented 4 years ago

Why is not works?

<?php

namespace App\Widgets;

use Arrilot\Widgets\AbstractWidget;
use Illuminate\Support\Facades\Log;

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

    public function __construct()
    {
        $this->addConfigDefaults([
            'page' => null,
        ]);
    }

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        Log::info($this->config['page']);
    }
}

@widget('customMenu',['page' => 'company'])

Result: Log.info: array('page' => null)

AlexThousand commented 4 years ago

I'm using Laravel 7

fabdelgado commented 4 years ago

Hi @AlexThousand

The correct method is this. {{ Widget::run('customMenu', [], $variable1, $variable2) }}

Could you check this, @widget('customMenu',[], ['page' => 'company'])

fabdelgado commented 4 years ago

Its works, @AlexThousand?

gunmanix commented 4 years ago

It does not work with Laravel 7, i got the following exception:

@widget('recentNews', [], 'test') Facade\Ignition\Exceptions\ViewException Unable to resolve dependency [Parameter #0 [ <required> $name ]] in class App\Widgets\RecentNews

But it's possible to passing the variables over the config array.

nileshd commented 4 years ago

Getting same error as @gunmanix , when I try to pass variables to the widget in Laravel 7..

@widget('DisplayValues', [], $properties)

Unable to resolve dependency [Parameter #0 [ $properties ]] in class App\Widgets\DisplayValues

Only config variables work in laravel7..

codemis commented 4 years ago

Current Solution

I found a solution, but I am not sure if it is the way you intended to use these arguments. If I call the widget with:

@widget('recentNews.recentNews', [], 'Afganisthan')

You can access the parameter in the run method like this:

public function run()
    {
        $country = func_get_arg(0);
        return view('widgets.recent_news.recent_news', [
            'country' => $country,
        ]);
    }

Was this the intended behavior? If so, then the README should be updated to reflect it.

The Problem

The README states:

@widget('recentNews', ['count' => 10], 'date', 'asc')
...
public function run($sortBy, $sortOrder) { }
...

If this is the intended behavior, then this may help with discovering the problem.

When the parameters are sent to the LaravelApplicationWrapper::call() method, they look like this:

Array ( [0] => date [1] => sortOrder )

It then calls the Container::call() method for Laravel, and eventually tries to find the dependency in BoundMethod::addDependencyForCallParameter() method. Since the array does not have a key with the name 'sortBy' or 'sortOrder', it is unable to match up the dependency with your widget run method. So it throws the error in line 180.

One possible fix is to use an array instead:

@widget('recentNews', ['count' => 10], ['sortBy' => 'date', 'sortOrder' => 'asc'])
...
public function run($sortBy, $sortOrder) { }
... 

However, I haven't found a way to implement this. In this situation, for some reason the array gets wrapped into an array, like this:

Array ( [0] => Array ( [sortBy] => date [sortOrder] => asc ) ) 

Which also cannot match up to the dependency. When I change the call method to this, it works:

return $this->app->call($method, $params[0]);

I hope this helps!

sebastienheyd commented 4 years ago

Hi!

I just send a PR to fix this issue by dynamically assigning the values to an associative array that will be used when calling the run method.

arrilot commented 4 years ago

Unfortunately, Laravel has changed the behavior of method injection, so variables cannot be passed directly to run method in n Laravel >=7.

Please configure widgets using config array. Documentation is updated