Giter Site home page Giter Site logo

Comments (5)

FatihKoz avatar FatihKoz commented on August 26, 2024

I may have some more info on this @arrilot , check below example please and hope it helps.
(pls do not mind the stupidity in the example, it is just there to explain the output)

namespace App\Widgets;

use App\Contracts\Widget;
use App\Repositories\NewsRepository;

class LatestNews extends Widget
{
  protected $config = ['count' => 5];

  public function run()
  {
    $newsRepo = app(NewsRepository::class);

    return view('widgets.latest_news', [
      'config' => $this->config,
      'news'   => $newsRepo->recent($this->config['count']),
    ]);
  }
}

Works just fine and config/count gets updated. However something like below makes config array changes impossible with widget calls.

namespace App\Widgets;

use App\Contracts\Widget;
use App\Repositories\NewsRepository;
use Illuminate\Support\Facades\Auth;

class LatestNews extends Widget
{
  protected $config = ['count' => 5];

  public function GetUser() {
    $user = Auth::user();
    return isset($user) ? $user->name_private : 'Dear Visitor';
  }

  public function run()  {
    $newsRepo = app(NewsRepository::class);
    $user = $this->GetUser();

    return view('widgets.latest_news', [
      'config' => $this->config,
      'user'    => $user,
      'news'   => $newsRepo->recent($this->config['count']),
    ]);
  }
}

So in simple terms, if I define anything more than public function run() in widget controller and use them, config array remains unchanged by widget calls and always the defaults are being returned.

If I move all the additional functions inside run() , config array starts acting normal.

from laravel-widgets.

arrilot avatar arrilot commented on August 26, 2024

Unfortunately I don't see any possible reasons why such behavior can happen.
I also cannot reproduce the issue on a fresh Laravel install.
You can try to reproduce it yourself and upload to a public github repo.

Wild guess: maybe you actually run your app on Laravel Octane instead of Apache.

from laravel-widgets.

arrilot avatar arrilot commented on August 26, 2024

I also noticed that you used your own widget class in extends Widget
Please make sure that you do not do any bad stuff there.

If you override AbstractWidget constructor you should call parent::__construct($config) in the end of the new __construct

from laravel-widgets.

FatihKoz avatar FatihKoz commented on August 26, 2024

Thanks for the reply, the main app is not using Laravel Octane and this is the Widget class we have in the app.

namespace App\Contracts;

use Arrilot\Widgets\AbstractWidget;

abstract class Widget extends AbstractWidget
{
    public $cacheTime = 0;

    public function view(string $template, array $vars = [])
    {
        return view($template, $vars);
    }
}

Will try using AbstractWidget to see if the Widget class is causing this behavior.

from laravel-widgets.

FatihKoz avatar FatihKoz commented on August 26, 2024

It was the __construct , thanks for pointing me out to the right direction... When I was defining additional functions, I was always using construct :)

  public function __construct(
    AirportRepository $airportRepo,
    array $config = []
  ) {
    $this->airportRepo = $airportRepo;
    parent::__construct($config);
  }

Closing this now, thanks again for your time and replies.

from laravel-widgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.