定制 brandembassy/slim-nette-extension 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

brandembassy/slim-nette-extension

Composer 安装命令:

composer require brandembassy/slim-nette-extension

包简介

README 文档

README

CircleCI Total Downloads Latest Stable Version

Nette Extension for integration of SLIM for API

This extension brings the power of Slim for applications using Nette DI. It enables you to easily work with Slim middleware stack and develop your API easily.

This package now uses Slim Framework 4.x

The general idea has been discussed in this article. (Czech language)

Philosophy

Single Responsibility

The main idea is to delegate responsibilities of the code handling requests to separated middlewares. For example:

  • authentication
  • validation
  • business logic

How middlewares in Slim work is described here.

Easy configuration

Empowered by Nette DI and it's neon configuration syntax this package provides powerful and easy way to define your API.

Usage

So let's start!

composer require brandembassy/slim-nette-extension

Extension

Now register new extension by adding this code into your config.neon:

extensions:
    slimApi: BrandEmbassy\Slim\DI\SlimApiExtension # Register extension

slimApi: # Configure it
    slimConfiguration:
        settings:
            removeDefaultHandlers: true # It's recommended to disable original error handling 
                                        # and use your own error handlers suited for needs of your app. 

    apiDefinitionKey: api # Your API definition will be under this key in "parameters" section. 

First API endpoint

Now let's say you want to make a REST endpoint creating channels, [POST] /new-api/2.0/channels

You need to define in parameters.api section in config.neon.

Both services and middlewares must be registered services in DI Container.

slimApi:
    handlers:
        notFound: App\NotFoundHandler # Called when not route isn't matched by URL
        notAllowed: App\NotAllowedHandler # Called when route isn't matched by method
        error: App\ApiErrorHandler # Called when unhandled exception bubbles out

    routes:
        "2.0": # Version of your API
            "channels": # Matched URL will be "your-domain.org/2.0/channels"
                post:
                    # This is service will be invoked to handle the request
                    service: App\CreateChannelAction
                    
                    # Here middleware stack is defined. It's evaluated from bottom to top. 
                    middlewares:
                        - App\SomeOtherMiddleware # last in row
                        - App\UsuallyRequestDataValidationMiddleware # second in row
                        - App\SomeAuthMiddleware # this one is called first 

    beforeRouteMiddlewares:
        # this is called for each route, before route middlewares
        - App\SomeBeforeRouteMiddleware 
        
    afterRouteMiddlewares:
        # this is called for each route, after the route middlewares
        - App\SomeAfterRouteMiddleware

    beforeRequestMiddlewares:
        # this is called for each request, even when route does NOT exist (404 requests)
        - App\SomeBeforeRequestMiddleware

You can also reference the named service by its name.

See tests/SlimApplicationFactoryTest.php and tests/config.neon for more examples.

Execution

Now you can simply get SlimApplicationFactory class from your DI Container (or better autowire it), create app and run it.

$factory = $container->getByType(SlimApplicationFactory::class);
$app = $factory->create();
$app->run();

Migrating from Slim 3 to Slim 4

Version 4.x of this package uses Slim Framework 4 instead of Slim Framework 3. The migration should be mostly transparent for users as the package maintains backward compatibility where possible.

Key Changes

  1. Dependencies: Slim 4 uses PSR-7, PSR-15, and PSR-17 standards more strictly
  2. Container: Slim 4 no longer provides its own container, but the package provides a compatibility layer
  3. Middleware: The double-pass middleware style ($request, $response, $next) is still supported
  4. Routing: Routes are now registered on the App instance directly (handled internally by the package)

What You Need to Do

For most users, the upgrade should be seamless:

  1. Update your composer.json to require the new version
  2. Run composer update brandembassy/slim-nette-extension
  3. Clear your cache directories (temp/, tests/temp/)
  4. Test your application

Breaking Changes

  • Response and ResponseInterface wrappers have been removed — route handlers now receive a plain PSR-7 ResponseInterface directly
  • RequestInterface::getRoute() now returns ?Route (nullable)
  • RequestInterface gained new methods: getInnerRequest(), getRoutingResults()
  • If you were directly accessing Slim internals (like Slim\Container or Slim\Router), you'll need to update your code
  • Custom middleware that relied on Slim 3 specific features may need updates
  • $app->run() and $app->handle() now apply a Content-Type fix for empty responses

Backward Compatibility

The following are maintained for backward compatibility:

  • Middleware signature remains the same (double-pass style $request, $response, $next)
  • Route handler signature remains the same ($request, $response)
  • Route and handler registration via NEON configuration is unchanged
  • Container access via $app->getContainer() works as before

For more details on Slim 4 changes, see the official Slim 4 upgrade guide.

brandembassy/slim-nette-extension 适用场景与选型建议

brandembassy/slim-nette-extension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 201.49k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2018 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 brandembassy/slim-nette-extension 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 201.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-17