hoangphi/hwa-tools

此包已被弃用且不再维护。作者建议使用 hwavina/hwa-meta 包。

关于辅助函数和元工具的包。由 Phi Hoang 开发。


README

68747470733a2f2f68776176696e612e636f6d2f6173736574732f696d616765732f6c6f676f2d312e706e67

Total Downloads Latest Stable Version License

关于

hwa-tools 是一个辅助包。它通过预构建的函数帮助我们更快地构建和开发。这为未来的项目节省了大量时间。

我们分享这个包是为了给程序员提供额外的实用库。我们希望人们使用这个包不是为了商业化或任何形式的盈利。

最后,希望从大家那里得到更多的贡献和分享。

非常感谢 🧡

安装

  1. 您可以通过 composer 安装此包
composer require hoangphi/hwa-tools
  1. 可选:服务提供者将自动注册。或者您可以在您的 config/app.php 文件中手动添加服务提供者
'providers' => array(
    // ...
    HoangPhi\HwaTools\Providers\HwaToolServiceProvider::class,
);
  1. 您可以自定义 config/hwa_tools.php 配置文件。如果您自定义了 config/hwa_tools.php 文件,您需要运行以下命令来清除缓存并更新更改。
php artisan config:cache

# or

php artisan optimize:clear

默认配置文件内容

您可以在以下位置查看默认配置文件内容:

https://github.com/hoangphidev/hwa-tools/blob/master/config/hwa_tools.php

使用方法

辅助函数

由于这是一个包含辅助函数的包,安装到项目中后,人们可以在项目的任何地方调用辅助函数。

以下是我们在其中构建的一些辅助函数:

  1. 通用
app_name(); // Get website name, project name - Ex: Laravel

activate_status(); // Get status activate - Ex: 1 - activate

deactivate_status(); // Get status deactivate - Ex: 2 - deactivate

gender_male(); // Get gender male - Ex: 1 - male

gender_female(); // Get gender female - Ex: 1 - female

page_limit(); // Get page limit default in pagination - Ex: 12

check_active_menu($menu, $url); // Check active menu. $menu is router menu
  1. 日期时间
current_day(); // Get current day - Ex: 01

current_month(); // Get current month - Ex: 07

current_year(); // Get current year - Ex: 2021

current_date(); // Get current - Ex: 2021-07-01 00:00:01

current_date('H:i:s d/m/Y'); // Get current date with format - Ex: 00:00:01 01/07/2021

custom_date('2021-07-01 00:00:01', 'H:i:s d/m/Y'); // Get customer date with format - Ex: 00:00:01 01/07/2021

custom_date('2021-07-01 00:00:01', 'H:i:s d/m/Y', 'en_EN'); // Get customer date with format and locale for diffForHumans - Ex: 00:00:01 01/07/2021 or 1 seconds or 1 minutes.

get_list_timezones(); // Get list timezones - response array
  1. 图片
storage_folder_path('users'); // Check and create folder if not existed in storage.

storage_image_path('users', '1.jpg') // Check and get image from image storage path.

storage_image_url('users', '1.jpg') // Get public asset url image from storage.
  1. IP 信息
get_user_agent(); // Get user agent

get_current_ip(); // Get current client ip

get_ip_info('127.0.0.1'); // Get info ip from http://api.ipstack.com

get_client_ip(); // Get client ip from https://ipinfo.io

code_to_country('EN'); // Convert country code to country name. Ex: EN to English

get_os(); // Get OS client using

get_client_browser(); // Get client browser using

get_device(); // Get client device using

get_country_list(); // Get list country world

get_list_currency(); // Get list currency
  1. 响应 API
send_ok_response('Message.', ['name' => 'Phi Hoang', 'phone' => '0989324221']); // Response normal with param 1 is message, param 2 is object or array data.

send_ok_response('Message.', '<paginate_items_object>', true); // Response with param 1 is message, param 2 is data paginate include results with meta, param 3 is boolean true to paginate.

send_created_response('Message.', 1); // Response with code 201 - param 1 is message, param 2 is id.

send_error_response('Message', 400); // Response error with code 400 is default. Message compare with code [401, 404, 403, 500].

元工具

此工具可以帮助您轻松管理某些对象的扩展数据字段。

  1. 使用命令创建元类。

示例:我们需要 CustomerMeta。此工具帮助我们生成模型文件和迁移文件。

php artisan hwa:make:meta Customer -m

我们添加了上述命令中的 -m 来创建迁移文件。

  1. 将允许类型添加到 config/hwa_tools.php
'allow_type' => [
    .....
    'customer' => ['id', 'customer_id'],
],
  1. 您需要运行以下命令来清除缓存并更新更改。
php artisan config:cache

# or

php artisan optimize:clear
  1. 运行命令以将元迁移文件迁移到数据库
php artisan migrate

完成上述配置步骤后,您可以在所需的类中使用的元模型中调用可用的方法。

// Example
CustomerMeta:_update(1, 'gender', 'male'); // update or add new gender for customer has id is 1

除了我们构建的函数和方法之外,人们还可以通过继承我们的类来构建自己的。

更新日志

请参阅 CHANGELOG 了解最近发生了哪些变化。

安全

如果您在 hwa-tools 中发现安全漏洞,请通过 hoangphidev@gmail.com 发送电子邮件给 Phi Hoang。所有安全漏洞都将得到及时处理。

致谢

许可证

Laravel 框架是开源软件,遵循 MIT 许可证