brandembassy / slim-nette-extension
Package info
github.com/BrandEmbassy/slim-nette-extension
pkg:composer/brandembassy/slim-nette-extension
Requires
- php: >=8.2
- ext-apcu: *
- adbario/php-dot-notation: ^2.2 || ^3.0
- nette/di: ^3.0
- nette/neon: ^3.0
- nette/utils: >=3.2
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- slim/psr7: ^1.6
- slim/slim: ^4.14
Requires (Dev)
- brandembassy/coding-standard: ^14.3
- marc-mabe/php-enum-phpstan: ^3.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^11.5
- rector/rector: ^2.1
- roave/security-advisories: dev-master
- spaze/phpstan-disallowed-calls: ^4.7
- dev-master
- 6.0.1
- 6.0
- 5.x-dev
- 5.11.x-dev
- 5.11
- 5.10
- 5.9
- 5.8
- 5.7
- 5.6
- 5.5
- 5.4.2
- 5.4.1
- 5.4
- 5.3
- 5.2
- 5.1-alpha
- 5.0.1-alpha
- 5.0-alpha
- 4.x-dev
- 4.3
- 4.2
- 4.1
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-DE-154553-v7-psr7-types
- dev-DE-154553-lazy-route-resolution
- dev-DE-154553-json-response-helper
- dev-copilot/upgrade-slim-version-4
- dev-DE-154553-v7-remove-legacy-bridges
- dev-DE-154553-slim4-composition-refactoring
- dev-DE-160171-php-8.4-compat
- dev-DE-154553-pr3-hollow-request
- dev-DE-154553-pr2-hollow-response
- dev-DE-154553-pr1-slim4-core
- dev-v6-dev
- dev-DE-154553-prep-d-reroute-to-json-response
- dev-DE-154553-prep-b-deprecation-annotations
- dev-DE-154553-prep-c-json-response-helper
- dev-DE-154553-prep-a-bugfix-array-merge
- dev-DE-154553-response-use-guzzle-psr7
- dev-DE-154553-remove-unused-response-methods
- dev-DE-154553-response-return-type-to-psr7
- dev-DE-154553-remove-response-wrapper
- dev-dev-fix-slim-http-request-error-again
- dev-DE-150589-request-update
- dev-dev-v5-transition-branch
- dev-dev-v5-transition-branch-try-route
- dev-DE-145427-merge-changes-from-v4-to-v5
- dev-DE-148371-fix-pipelines
- dev-DE-125675-sonar
- dev-DE-125675
- dev-add-after-route-middlewares-option
- dev-add-swoole-bridge
- dev-DE-55463-SonarQubeIntegration
- dev-update-brandembassy-datetime
- dev-test-for-parsed-body
- dev-upgrade_libraries
This package is auto-updated.
Last update: 2026-03-23 15:19:20 UTC
README
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
- Dependencies: Slim 4 uses PSR-7, PSR-15, and PSR-17 standards more strictly
- Container: Slim 4 no longer provides its own container, but the package provides a compatibility layer
- Middleware: The double-pass middleware style (
$request, $response, $next) is still supported - 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:
- Update your
composer.jsonto require the new version - Run
composer update brandembassy/slim-nette-extension - Clear your cache directories (
temp/,tests/temp/) - Test your application
Breaking Changes
ResponseandResponseInterfacewrappers have been removed — route handlers now receive a plain PSR-7ResponseInterfacedirectlyRequestInterface::getRoute()now returns?Route(nullable)RequestInterfacegained new methods:getInnerRequest(),getRoutingResults()- If you were directly accessing Slim internals (like
Slim\ContainerorSlim\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.