A
A
Annywebart2016-06-15 00:07:11
Laravel
Annywebart, 2016-06-15 00:07:11

Widget problem. What is the best way to organize widgets in Laravel 5.2?

The site has a system of widgets, which I found on the English Internet.
In the app folder, I created the "Widgets" folder, the structure looks like this:
260649c2699546ad83c2372837f6b62a.PNG
At the root of the "Widgets" folder, I created the ServiceProvider.php file with the following content:

namespace App\Widgets;

class ServiceProvider extends  \Illuminate\Support\ServiceProvider
{
  public function boot()
  {
    $widgets = config("widget.widgets");
    while (list(,$widget) = each($widgets)) {
      if(is_dir(__DIR__.'/'.$widget.'/Views')) {
        $this->loadViewsFrom(__DIR__.'/'.$widget.'/Views', 'widget.' . $widget);
      }
    }
  }
  
  public function register(){
    
  }
}

In the config folder, I created a widgets.php file with the following content:
return [
  'widgets' => [
    'menu',
    'slider',
    'carousel',
    'reviews',
    'cart',
    'wishlist',
    'viewed',
    'articles',
  ],
];

Widget code example:
namespace App\Widgets\Slider;

class Slider
{
  public function show()
  {
    $items = \App\Models\Slider::whereIsPublished(1)->get();

    return view('widget.slider::index', compact('items'));
  }
}

The widget is called $slider = new Slider();and in the right place $slider->show();
On the local (Xampp with PHP 5.6 version) everything works fine, but I threw the site on shared hosting with PHP 7.0.7 and there is this error:
ErrorException in FileViewFinder.php line 112: No hint path defined for [widget.slider]. (View: /home/wearsh00/wear-shop.pp.ua/project/resources/views/index.blade.php)

Tell me please. Is this widget system normal, or can it be made more beautiful and simpler?
And what is causing this error? I've searched a lot, but haven't found a solution.
Just in case, hosting settings: php70.demo.default-host.net
Thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denwebart, 2016-06-16
@Annywebart

The problem is a case mismatch. On windows it is ignored, but not on shared hosting.
Solved by fixing the case in Widgets/ServiceProvider.php like this:

if(is_dir(__DIR__. '/' . ucfirst($widget) . '/Views')) {
    $this->loadViewsFrom(__DIR__. '/'. ucfirst($widget) . '/Views', 'widget.' . $widget);
}

T
Tesla, 2016-06-15
@Tesla

Did you search right? There is a ready-made working solution arrilot/laravel-widgets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question