定制 mhaggag/laravel-weather 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mhaggag/laravel-weather

Composer 安装命令:

composer require mhaggag/laravel-weather

包简介

A Laravel package for fetching weather data via Open-Meteo API.

README 文档

README

A robust Laravel package to fetch, cache, and format weather data using the Open-Meteo API. It supports multi-language suggestions (English & Arabic), unit conversion, and flexible configuration.

🌟 Features

  • Current, Hourly & Daily Weather: Fetch comprehensive weather data.
  • Localization: Built-in support for English (en) and Arabic (ar).
  • Smart Suggestions: Get human-readable advice based on weather conditions (e.g., "Take an umbrella").
  • Caching: Automatic caching of API responses to improve performance.
  • Units: Supports Metric (°C, km/h) and Imperial (°F, mph) systems.
  • Resilient: Handles API timeouts and retries automatically.

📦 Installation

1. Install via Composer

composer require mhaggag/laravel-weather

2. Publish Configuration & Translations

Publish the configuration file and translation resources to your application:

php artisan vendor:publish --provider="MHaggag\LaravelWeather\WeatherServiceProvider"

# Or publish specific resources:
php artisan vendor:publish --tag="weather-config"

php artisan vendor:publish --tag="weather-translations"

php artisan vendor:publish --tag="weather-views"

This will publish:

  • config/weather.php
  • lang/vendor/weather (for customizing translations)
  • resources/views/vendor/weather (for customizing the demo view)

3. Environment Configuration

Add the following to your .env file to customize defaults:

OPEN_METEO_URL=https://api.open-meteo.com/v1
WEATHER_CACHE_TTL=300
WEATHER_UNITS=metric
WEATHER_TIMEZONE=auto
WEATHER_HTTP_TIMEOUT=8
WEATHER_ENABLE_DEMO=false

🚀 Usage

You can use the Weather facade (the alias is automatically registered) or inject the MHaggag\LaravelWeather\Services\WeatherService into your controllers and classes.

Get Current Weather

Returns a simplified array with current weather conditions, formatted according to your locale and units.

use MHaggag\LaravelWeather\Facades\Weather;

public function index()
{
    // Get current weather for Cairo (Lat: 30.0444, Lon: 31.2357)
    $current = Weather::current(30.0444, 31.2357);
    
    // With options (e.g., Arabic output)
    $currentAr = Weather::current(30.0444, 31.2357, ['locale' => 'ar']);
    
    return $current;
}

Output Example:

{
    "temperature": "25.0 °C",
    "windspeed": "15.0 km/h",
    "condition": "Clear",
    "icon": "sun",
    "time": "2023-10-05T12:00"
}

Get Raw Data

Fetch the raw response from Open-Meteo if you need to process it manually.

$rawData = Weather::get(30.0444, 31.2357, [
    'hourly' => ['temperature_2m', 'rain'],
    'daily' => ['temperature_2m_max']
]);

And format data according to your needs.

$data = Weather::format($rawData, [
    'units' => 'imperial',
    'locale' => 'en'
]);

Or Do both in One Step

Get Formatted Data (Current + Hourly + Daily):

$data = Weather::getAsFormatted(30.0444, 31.2357, [
    'units' => 'imperial',
    'locale' => 'en'
]);

Weather Suggestions

Get a helpful message based on the weather data.

$suggestion = Weather::getSuggestion($rawData, ['locale' => 'en']);
// Output: "Clear skies and high temperature — stay hydrated if heading out."

⚙️ Configuration

Check config/weather.php for advanced settings.

Custom Suggestions

You can define custom rules for suggestions in the config file. The service evaluates rules in order and returns the message of the first match.

'suggestions' => [
    [
        'condition' => 'Rain',
        'message' => 'Don\'t forget your umbrella!',
    ],
    [
        'min_temp' => 35,
        'message' => 'It\'s scorching hot outside!',
    ],
],

Sample View Screenshots

You can enable the demo view in config/weather.php to see the sample view.

English Arabic
Sample view as English Sample view as Arabic

🧪 Testing

Run the package tests:

composer test

📄 License

The MIT License (MIT).

mhaggag/laravel-weather 适用场景与选型建议

mhaggag/laravel-weather 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mhaggag/laravel-weather 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mhaggag/laravel-weather 我们能提供哪些服务?
定制开发 / 二次开发

基于 mhaggag/laravel-weather 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-14