bitpress/blade-extensions
最新稳定版本:1.0.0
Composer 安装命令:
composer require bitpress/blade-extensions
包简介
Laravel Blade extension classes
README 文档
README
This Laravel >=5.5 package allows you to organize your Blade extensions into classes. It provides a simple container pass that registers all your extensions with the Blade compiler using a simple tag.
Organizing Blade extensions as classes in the service container allows you to group extension functionality within one object, allowing you to inject dependencies through the service container and provide shared protected/private methods.
Installation
You can install this package via Composer using the following command:
composer require bitpress/blade-extensions
This package will automatically register the included service provider.
Usage
At a high level, the goal of this package is to make it easy and convenient to register blade extensions as classes from the service container automatically using service container tagging. Here's the gist of how it works:
- Create a new Extension class with
php artisan make:blade Example - Register the Extension in a service provider's
register()method - Tag the service with
blade.extension - The
BladeExtensionServiceProviderautomatically wires up the directives in the blade compiler duringboot().
Creating a new Extension Class
It would be annoying to create a new extension class from scratch each time, so this package provides an artisan command to create your extensions under the App\Blade namespace:
# Creates App\Blade\CartExtension
php artisan make:blade Cart
Defining the Extension Directives and Conditionals
Once you create a blade extension class, you can define supported directives, conditionals, or both. Custom Blade directives and conditionals are simply PHP callables:
<?php namespace App\Blade; use BitPress\BladeExtension\Contracts\BladeExtension; class CartExtension implements BladeExtension { public function getDirectives() { return [ 'cartcount' => [$this, 'getCartCount'] ]; } public function getConditionals() { return [ 'cartempty' => [$this, 'isCartEmpty'] ]; } public function getCartCount() { // logic to return cart count } public function isCartEmpty() { // logic for empty cart } }
Note that Blade extension classes implement the BladeExtension contract, which includes getDirectives() and getConditionals(). Even if you don't plan on registering any conditionals, for example, you must implement the getConditionals() method and return an empty array.
Your custom blade extension will be registered in the service container, so you can define a __construct() method and inject services from the container. The fact that Blade extensions are services allows you to group common code around your blade extensions, including those directives that depend on outside services that are registered in the container.
Registering a New Blade Service
After you define a blade extension, you still need to register it in the service container and tag it properly so the BladeExtensionServiceProvier can define the PHP callables in the Blade compiler during boot().
This package provides a BladeRegistrar class to define the extension in the container and tagging it properly, for example, in App\Providers\AppServiceProvider:
use Illuminate\Support\ServiceProvider; use BitPress\BladeExtension\Container\BladeRegistrar; class AppServiceProvider extends ServiceProvider { public function register() { BladeRegistrar::register(\App\Blade\CartExtension::class); } }
If you need to, you can create the service in a Closure:
use \App\Blade\CartExtension::class; BladeRegistrar::register(CartExtension::class, function () { // Do stuff... return new CartExtension($stuff); });
You can also use the blade_extension() helper function to register the service if you prefer:
public function register() { blade_extension(\App\Blade\CartExtension::class); }
Without the Blade extension registrar, this is how you'd define an extension manually:
$this->app->singleton(\App\Blade\CartExtension::class); $this->app->tag(\App\Blade\CartExtension::class, 'blade.extension');
The benefit of using the BladeRegistrar class is that it takes care of defining a consistent tag across any number of service providers and removes the boilerplate.
Registering Extensions with the Blade Compiler
The way this package works is quite simple, here's how the boot() method actually gets all registered extensions and defines them with the Blade compiler:
foreach ($this->app->tagged('blade.extension') as $extension) { if (! $extension instanceof BladeExtension) { throw new InvalidBladeExtension($extension); } foreach ($extension->getDirectives() as $name => $callable) { $this->app['blade.compiler']->directive($name, $callable); } foreach ($extension->getConditionals() as $name => $callable) { $this->app['blade.compiler']->if($name, $callable); } }
License
The MIT License (MIT). Please see License File for more information.
bitpress/blade-extensions 适用场景与选型建议
bitpress/blade-extensions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.26k 次下载、GitHub Stars 达 136, 最近一次更新时间为 2017 年 11 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「blade」 「bitpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bitpress/blade-extensions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bitpress/blade-extensions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bitpress/blade-extensions 相关的其它包
同方向 / 同关键字的高下载量 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.
统计信息
- 总下载量: 10.26k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 136
- 点击次数: 21
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-30