bhuvidya/laravel-blade-helper
Composer 安装命令:
composer require bhuvidya/laravel-blade-helper
包简介
A Laravel 5 package to provide an abstraction when creating Blade directives.
README 文档
README
A Laravel 5 package to ease the creation of Blade directives.
Please note that this package was tested on Laravel 5.6. It should work on >=5.5, but I cannot guarantee it will work on earlier versions. Sorry.
BIG KUDOS to Liam (https://github.com/ImLiam) who proposed this as a pull request on the Laravel Framework (laravel/framework#24923). I really liked the concept so I put his code into a package for others to use if they wanna. This README draws on his explanations in the pull request.
Installation
Add bhuvidya/laravel-blade-helper to your app:
$ composer require "bhuvidya/laravel-blade-helper"
If you're using Laravel 5.5 or higher, you don't have to edit app/config/app.php.
Otherwise, edit app/config/app.php and add the service provider:
'providers' => [
'BhuVidya\BladeHelper\BladeHelperServiceProvider',
]
Configuration
The package comes with its own configuration file, which you can install and tweak in your application:
artisan vendor:publish --provider='BhuVidya\BladeHelper\BladeHelperServiceProvider' --tag=config
This will install the config file to your app's config directory. It's contents are:
return [
/*
|--------------------------------------------------------------------------
| Service instance "name"
|--------------------------------------------------------------------------
*/
'instance' => 'bhuvidya.blade_helper',
/*
|--------------------------------------------------------------------------
| You can elect to register an alias for the facade automatically, and give
| it your own custom class name. Set to false to not register.
|--------------------------------------------------------------------------
*/
'facade' => 'BladeHelper',
];
As you can see, it's possible to customise the service container instance name, and (probably a bit more useful), get the facade to be loaded automatically, and with your own class name if so desired.
Usage
Basically this helper provides a neat level of abstraction to the "define a custom Blade directive" process. It's benfit is in allowing the connected function (or closure) to have a well-defined parameter list, without having to do the cruft coding around parsing the raw expression string passed in from Laravel core. This allows you to quickly turn an external function into a Blade directive.
For example, to turn the php function join into a directive:
// Define the helper directive
BladeHelper::helper('join');
// Use it in a view
@join('|', ['Hello', 'world'])
// Get the compiled result
<?php echo join('|', ['Hello', 'world']); ?>
// See what's echoed
"Hello|world"
Admittedly a contrived example, but it gives you the idea.
The second argument can also take a callback. The advantage of a callback here over the Blade::directive(...) method is that the closure can have specific parameters defined instead of just the raw expression passed through. This has several good things that solve a previous idea Liam brought up:
- Type hint the arguments for the callback
- Manipulate and use the individual arguments when the directive is called, instead of the raw expression as a string
- Define a directive without having to only use it as a proxy to a helper function or class in another part of the application
// Define the helper directive
BladeHelper::helper('example', function($a, $b, $c = 'give', $d = 'you') {
return "$a $b $c $d up";
});
// Use it in a view
@example('Never', 'gonna')
// Get the compiled result
<?php echo \Illuminate\Support\Facades\Blade::getHelper('example', 'Never', 'gonna'); ?>
// See what's echoed
"Never gonna give you up"
By default, all of the helper directives will echo out their contents to the view when used. This can be disabled by passing false as the third argument.
// Define the helper directive
BladeHelper::helper('log', null, false);
// Use it in a view
@log('View loaded...')
// Get the compiled result
<?php log('View loaded...'); ?>
// Nothing is echoed
In the pull request there was some discussion around whether directives should be used just for code structure, and not presentation. Personally I like custom directives even for "presentation" or "convenience" usage because:
- It makes template files cleaner, and easier to read
- Having the code in one place makes it easier to change in the future
As a last example, here is a wrapper for the logic around a FontAwesome 4 icon. There's a bit of boilerplate around it you probably don't want to remember to write every time, and you wouldn't want to @include it every time. While it could be a regular function and operate just the same, this is one of those things you'd probably want that bit of syntactic sugar around because of how often it can be used - nor would there be any advantage of it being a regular function or helper as you'd never get any use of it outside views. Also, if you ever decided to tweak the html of the <i> element (maybe adding a new aria attribute), then you only have to do it in one place.
BladeHelper::helper('fa', function(string $iconName, string $text = null, $classes = '') {
if (is_array($classes)) {
$classes = join(' ', $classes);
}
$text = $text ?? $iconName;
return "<i class='fa fa-{$iconName} {$classes}' aria-hidden='true' title='{$text}'></i><span class='sr-only'>{$text}</span>";
});
bhuvidya/laravel-blade-helper 适用场景与选型建议
bhuvidya/laravel-blade-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「blade」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bhuvidya/laravel-blade-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bhuvidya/laravel-blade-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bhuvidya/laravel-blade-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Render blade templates into a PSR-7 Response object.
A tiny Flask-like PHP microframework where each public method is an endpoint and routes are inferred from method names.
The EdgestashBlade Laravel package adds Edgestash support to Blade. Edgestash is a Varnish Enterprise module that processes Mustache templates on the edge.
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-07