PHP code example of highvertical / widget-package

1. Go to this page and download the library: Download highvertical/widget-package library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

highvertical / widget-package example snippets

bash
php artisan vendor:publish --tag=widget-config
bash
'providers' => [
    // Other Service Providers...

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


namespace App\Widgets;

use Highvertical\WidgetPackage\Widgets\Widget;

class WeatherWidget extends Widget
{
    public function render(array $params = [])
    {
        $location = $params['location'] ?? 'Unknown Location';

        // Mocked data, replace with real data fetching logic
        $weatherData = [
            'location' => $location,
            'temperature' => '25°C',
            'condition' => 'Sunny',
        ];

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


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


return [
    'widgets' => [
        'recentPosts' => \Modules\Blog\Widgets\RecentPostsWidget::class,
    ],
];
bash


namespace App\Widgets;

use App\Services\WeatherService;
use Highvertical\WidgetPackage\Widgets\Widget;

class WeatherWidget extends Widget
{
    protected $weatherService;

    public function __construct(WeatherService $weatherService)
    {
        $this->weatherService = $weatherService;
    }

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

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


namespace Modules\Blog\Widgets;

use Highvertical\WidgetPackage\Widgets\Widget;
use Modules\Blog\Repositories\PostRepository;

class RecentPostsWidget extends Widget
{
    protected $postRepository;

    public function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }

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

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


namespace App\Widgets;

use Highvertical\WidgetPackage\Widgets\Widget;
use App\Models\User;

class UserProfileWidget extends Widget
{
    public function render(array $params = [])
    {
        $user = User::find($params['user_id']);

        return view('widgets.user-profile', compact('user'));
    }
}