helsingborg-stad/blade 问题修复 & 功能扩展

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

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

helsingborg-stad/blade

Composer 安装命令:

composer require helsingborg-stad/blade

包简介

Use Laravel Blade in any PHP project.

README 文档

README

Contributors Forks Stargazers Issues License PHP 8.2

Logo

Blade

Use Laravel Blade in any PHP project.
If you don't know about Blade yet, please refer to the official documentation.

Report Bug · Request Feature

Requirements

  • PHP ^7.4 | ^8.0
  • Composer

Installation

composer require helsingborg-stad/Blade

Usage

Configuration

Cache path

  • The cache path can be set by passing the path to the constructor of the BladeService.
  • If no cache path is set, the Blade Service will use the system's temporary directory.
  • The cache path can be overriden by defining the BLADE_CACHE_PATH environment constant.

Initialize blade engine:

This can be done either as a local instance or as a global reusable instance. The global instance is recommended better performance and less memory usage.

Locally

$viewPaths    = ['path/to/view/files'];
$cachePath    = 'path/to/cache/files';
$bladeService = new BladeService($viewPaths, $cachePath);

Globally (for convenient reuse)

$viewPaths    = ['path/to/view/files'];
$cachePath    = 'path/to/cache/files';
$bladeService = GlobalBladeService::getInstance($viewPaths, $cachePath);

// You can now reuse the same instance by calling:
$sameInstance = GlobalBladeService::getInstance($viewPaths, $cachePath);

Note

If calling GlobalBladeService::getInstance with parameters after the first call, the $viewPaths will be added and the $cachePath parameter will be ignored.

Render view

$viewFile = 'foo.blade.php';
$html     = $bladeService->makeView($viewFile)->render();

Render view with variables

$viewFile = 'foo.blade.php';
$html = $bladeService->makeView($viewFile, ['name' => 'John Doe'])->render();

Render with specific view path

This enables you to temporarily use a specific view path without storing it in the Blade Service.

$viewFile = 'foo.blade.php';
$html = $bladeService->makeView($viewFile, ['name' => 'John Doe'], [], 'specific/view/path')->render();

Register a custom directive

$bladeService->registerDirective('datetime', function ($expression) {
    return "<?php echo with({$expression})->format('F d, Y'); ?>";
});

// The directive can now be used by adding the @datetime directive to a view file.

Register a component

$bladeService->registerComponent('foo', function ($view) {
    $view->with('name', 'John Doe');
});

// The component can now be used by adding @component('foo')@endcomponent to a view file.

Register a component directive

If you have already registered a component. That component can be added as a directive by doing the following:

$bladeService->registerComponentDirective('componentname', 'directivename');

// This will register a directive that can be used by adding @directivename()@enddirectivename to a view file, and it will output the component.

Add additional view file paths

If you need to add more view file paths after initializing the Blade Service, this can be done by calling BladeService::addViewPath

$bladeService->addViewPath('extra/view/path');

Prepend view file paths

If you need to add more view file paths before the existing view file paths, this can be done by calling BladeService::prependViewPath with the second parameter set to true.

$bladeService->prependViewPath('extra/view/path', true);

Important

For every unique view path added, performance will be affected. This is due to the fact that the Blade Service will have to search through all view paths to find the correct view file. Therefore, it is recommended to add as few view paths as possible.

Error handling

This package offers a convenient solution for swiftly addressing issues that arise within a function called in a view or a syntax error directly within a file. It provides a function to visually display the error, which proves particularly useful during the development process. To optimize development speed and efficiency, it's advisable to implement an error handler when invoking makeView, ensuring smooth troubleshooting whenever errors occur.

try {
    return $bladeService->makeView($viewFile, ['name' => 'John Doe'], [], 'specific/view/path')->render();
} catch (Throwable $e) {
    $bladeService->errorHandler($e)->print();
}

Testing

Unit tests

composer test

Code coverage

composer test:coverage

helsingborg-stad/blade 适用场景与选型建议

helsingborg-stad/blade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58.35k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 helsingborg-stad/blade 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 58.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 3
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-14