PHPackages                             highvertical/widget-package - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. highvertical/widget-package

ActivePackage

highvertical/widget-package
===========================

A simple and flexible widget package for Laravel. Compatible with Laravel 6.x and above.

1.1.3(1y ago)126MITPHPPHP ^7.2.5|^8.0

Since Aug 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/highvertical/widget-package)[ Packagist](https://packagist.org/packages/highvertical/widget-package)[ RSS](/packages/highvertical-widget-package/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (6)Used By (0)

Widget Package for Laravel
==========================

[](#widget-package-for-laravel)

Introduction
------------

[](#introduction)

The Widget Package is a simple yet powerful Laravel package that allows developers to create reusable widgets for their applications. These widgets can be used anywhere in Blade views, making it easier to manage complex UIs, build dynamic content, and maintain a clean separation of concerns. The package is designed to be compatible with both small and large projects, including modular applications like those built with nwidart/laravel-modules.

Features
--------

[](#features)

#### Simple Widget Creation:

[](#simple-widget-creation)

Create widgets that can be easily rendered in Blade views.

#### Caching:

[](#caching)

Built-in support for caching widget output to improve performance.

#### Dynamic Configuration:

[](#dynamic-configuration)

Define and manage widgets via a configuration file.

#### Modular Compatibility:

[](#modular-compatibility)

Fully compatible with modular applications, including those using nwidart/laravel-modules.

#### Advanced Blade Directives:

[](#advanced-blade-directives)

Use custom Blade directives to render widgets anywhere in your views.

Installation
------------

[](#installation)

- Step 1: Install the Package Install the package via Composer:

```
composer require highvertical/widget-package
```

- Step 2: Publish the Configuration File Publish the package's configuration file to your Laravel application's config directory:

```
php artisan vendor:publish --tag=widget-config
```

This command will create a config/widgets.php file in your application.

- Step 3: Autoload the Service Provider (Optional) If your application does not support package auto-discovery, add the service provider manually in config/app.php:

```
'providers' => [
    // Other Service Providers...

    Highvertical\WidgetPackage\Providers\WidgetServiceProvider::class,
],
```

Basic Usage
-----------

[](#basic-usage)

- Step 1: Create a Widget Widgets can be created anywhere in your application. Here's an example of a simple weather widget:

```
 $location,
            'temperature' => '25°C',
            'condition' => 'Sunny',
        ];

        return view('widgets.weather', compact('weatherData'));
    }
}
```

- Step 2: Register the Widget After creating the widget class, register it in your config/widgets.php file:

```
 [
        'weather' => \App\Widgets\WeatherWidget::class,
    ],
    'cache' => [
        'enabled' => true,
        'ttl' => 60, // Cache time-to-live in minutes
    ],
];
```

- Step 3: Use the Widget in a Blade View You can now use your widget in any Blade view with the custom @widget directive:

```
@widget('weather', ['location' => 'New York'])
```

Advanced Usage
--------------

[](#advanced-usage)

### Caching

[](#caching-1)

The Widget Package supports caching out of the box. To enable caching, simply ensure that the cache.enabled configuration is set to true in your config/widgets.php file:

```
'cache' => [
    'enabled' => true,
    'ttl' => 60, // Cache time-to-live in minutes
],
```

When caching is enabled, the widget output is stored and reused for subsequent requests within the specified time-to-live (TTL) period.

### Dynamic Widget Configuration

[](#dynamic-widget-configuration)

Widgets can be defined dynamically across different parts of your application, including within modular applications. This allows for more flexible and maintainable code, particularly in larger projects.

Example: Modular Widgets with nwidart/laravel-modules

If you are using nwidart/laravel-modules, you can define widgets within individual modules. For example, in a module named Blog, create a Config/widgets.php file:

```
 [
        'recentPosts' => \Modules\Blog\Widgets\RecentPostsWidget::class,
    ],
];
```

The package will automatically detect and register these widgets, making them available across your entire application.

### Handling Widget Dependencies

[](#handling-widget-dependencies)

Widgets can have dependencies that need to be resolved by Laravel's service container. You can easily inject these dependencies by defining a constructor in your widget class:

```
weatherService = $weatherService;
    }

    public function render(array $params = [])
    {
        $location = $params['location'] ?? 'Unknown Location';
        $weatherData = $this->weatherService->getWeather($location);

        return view('widgets.weather', compact('weatherData'));
    }
}
```

### Extending and Overriding Configuration

[](#extending-and-overriding-configuration)

To extend or override the default widget configuration, modify your config/widgets.php file. This is particularly useful if you are integrating the package into an existing project and need to adapt it to your specific needs.

### Blade Directives

[](#blade-directives)

The package registers a custom Blade directive @widget to make it easy to render widgets in your views. This directive takes the widget alias and an optional array of parameters.

```
@widget('weather', ['location' => 'San Francisco'])
```

Example Widgets
---------------

[](#example-widgets)

- Example 1: Recent Posts Widget

```
postRepository = $postRepository;
    }

    public function render(array $params = [])
    {
        $posts = $this->postRepository->getRecentPosts($params['limit'] ?? 5);

        return view('blog::widgets.recent-posts', compact('posts'));
    }
}
```

- Example 2: User Profile Widget

```
