repat/laravel-helper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

repat/laravel-helper

Composer 安装命令:

composer require repat/laravel-helper

包简介

Some helper function for developing with Laravel 5+

README 文档

README

Latest Version on Packagist Total Downloads

laravel-helper is a package full of helper functions I found useful when developing applications with Laravel. All functions are wrapped with a functions_exists() in case of conflicts.

Also have a look at

Ideas what should go in here? Write a pull request or email!

Installation

$ composer require repat/laravel-helper

Documentation

⚠️ The majority of helper functions are now in repat/php-helper which this package is based on. You can find the documentation at https://github.com/repat/php-helper

Database

mysql_headers($table, $assoc = false)

Returns an array of MySQL headers/columns or empty array in case of an error. If the second parameter is set true (default is false) it returns an associative array.

print_r(mysql_headers("test_table"));
// returns: Array( [0] => head1, [1] => head2 )

print_r(mysql_headers("test_table", $assoc = true));
// returns: Array( [head1] => head1, [head2] => head2)

table_headers($model)

Returns the database table headers, similar to mysql_headers(), but based on an object of a Eloquent Model.

use App\Models\User;

$user = User::first();

print_r(table_headers($user));
// returns: Array( 1 => id, 2 => name, ...)

print_db_session($table = 'sessions')

print_r() the session of current user.

print_db_session();
// returns:
// Array
// (
//     [_token] => 7Au0aYkJVxQVA3xQBfdJwKNaWxUv0UVJKublCqMn
//     [locale] => en
//     ...
// )

get_free_slug($toSlug, $field, $fqcn, $id, $pk)

Returns a unique slug for an Eloquent Model given the following parameters:

  • $toSlug: suggestion for the slug
  • $field: name of the database field, usually slug
  • $fqcn: Fully qualified class name of Eloquent Model
  • $id: id to exclude (e.g. it's own on update)
  • $pk: primary key of the database table, defaults to id

Will append a number if $toSlug is already taken.

use App\Model\User;

$user = User::first();

$user->id;
// returns: 1
$user->slug;
// returns: foobar

get_free_slug('foobar', 'slug', User::class, 1, 'id');
// returns: foobar1

insert_bindings($query)

Inserts values into ? from the ->toSql() string.

insert_bindings(DB::table('users')->where('id', 1));
// returns: SELECT * FROM `users` WHERE `id` = '1'

Object

morph_map()

Returns the morphMap from AppServiceProvider set with Relation::morphMap().

morph_map();
// returns:Array
// (
//     [user] => App\Models\User
// )

morph_map_key($fqcn)

Reverse lookup for a class in the morphMap of the AppServiceProvider set with Relation::morphMap().

use App\Models\User;

morph_map_key(User::class);
// returns: 'user'

cache_get_or_add($key, $callable)

Returns Cache for given key or adds the return value from the callable to the cache and then returns it.

use App\Models\Post;

$posts = cache_get_or_add('posts', function() {
    return Post::orderBy('created_at', 'desc')->get();
});

dispatch_tinker($job)

Dispatches jobs from the tinker REPL.

dispatch_tinker(new \App\Jobs\CleanupJob());
// returns: 1 (id of job)

Networking

route_path($path)

Get the path to the Laravel routes folder, similar to app_path(), see Helpers Documentation. It will append $path but it's not mandatory.

route_path();
// returns: /var/www/htdocs/laravel/routes

route_path('web.php');
// returns: /var/www/htdocs/laravel/routes/web.php

named_routes($path, $verb)

Returns array of all named routes in a routes file or null on error. It's possible to pass an HTTP verb/method defined in HTTP_VERBS_LARAVEL (see below).

named_routes('/var/www/htdocs/laravel/routes/web.php');
// returns: [
// 'laravel.get'
// 'laravel.post'
// ]

named_routes('/var/www/htdocs/laravel/routes/web.php', 'get');
// returns: [
// 'laravel.get'
// ]
current_route_name()

If the current route has a name, otherwise return null.

// in routes/web.php
// Route::name('dev.foo')->get('foo', 'Dev\TestController@foo');
// Route::get('bar', 'Dev\TestController@bar');

// in Dev/TestController@foo
current_route_name();
// returns: dev.foo

// in Dev/TestController@foo
current_route_name();
// returns: null
all_routes()

Returns an array of all routes like so:

all_routes();
// returns:
//      "name" => 'route.test', // could be null
//      "methods" => [
//          "GET",
//          "HEAD",
//      ],
//      "uri" => "test",
//      "action" => "\App\Http\Controllers\TestController@test",
route_exists($namedRoute)

Checks if the given route is a named route in any routes file.

route_exists('route.test');

// returns: true

route_exists('route.foobar')

// returns: false

Optional Packages

Optional packages suggested by this are required for these functions to work.

translated_attributes($fqcn)

Uses astrotomic/laravel-translatable and Reflection to get the translatedAttributes attribute of a Model.

  • $ composer require astrotomic/laravel-translatable
use App\Models\Product;

translated_attributes(Product::class);
// returns: ['title', 'description'];

HTML

extract_inline_img($text, $storagePath, $srcPath, $optimize)

Extracts an inline image from a text, saves it on the harddrive and puts in the filename with the src attribute. Can use the spatie/laravel-image-optimizer to optimize images after upload but it's disabled by default.

  • $ composer require spatie/laravel-image-optimizer
extract_inline_img("<img src='data:image/jpeg;base64,...>", '/var/www/htdocs/laravel/storage/foobar', 'public/images', true);
// returns: <img src="public/images/fj3209fjew93.jpg">

Constants

  • HTTP_VERBS_LARAVEL: [all, get, head, post, delete, options, put, patch]
  • REGEX_IMG_BASE64_SRC: Regular Expression used to find a base64 encoded image in HTML text
  • REGEX_IMG_BASE64_REPLACE: Regular Expression used to replace a base64 encoded image in HTML text
  • MULTIPLE_TRANS: 2

Contributors

License

Version

  • Version 0.6

Contact

repat

Flattr this git repo

repat/laravel-helper 适用场景与选型建议

repat/laravel-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.7k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「date」 「array」 「files」 「helper」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-09